本文介绍了null springSecurityService会导致encodePassword在Grails 2.0.1中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在与Grails 2.0.1和Spring Security合作开发一个新项目。由于用户域对象中的springSecurityService为空,创建用户上下文失败。奇怪的是,这只发生在我们的Linux文本框,而在所有的开发人员窗口框它工作正常。不知道它是否与环境有关,或者是否与别的环境有关。

We are working on a new project with Grails 2.0.1 and Spring Security. The create user context fails due to springSecurityService in the User domain object being null. Strangely this occurs only our Linux text box while on all the developer windows boxes it works fine. Not sure if it has anything to do with the environment or whether it is something else. On the linux box this fails consistently.

我们使用的用户域类在下面(插件生成的类带有几个附加字段)。 encodePassword是由beforeInsert(),beforeUpdate()触发器处理的。

The user domain class we are using is below (the plugin generated class with a couple of additional fields). The encodePassword is being handled by the beforeInsert(), beforeUpdate() triggers.

在这个线程中发现了一些关于webflows中导致问题的瞬态引用,没有在这里使用,所以不知道这是否相关。

Came across this thread which talks about the transient references causing problems in webflows, which I assume are not being used here, so not sure if this is relevant.http://grails.1312388.n4.nabble.com/Spring-Security-Plugin-1-of-the-time-springSecurityService-null-td4349941.html

class User {

    transient springSecurityService

    static constraints = {
        firstName blank: false, nullable: false, size: 2..100
        lastName blank: false, nullable: false, size: 2..100
        username blank: false, nullable: false, unique : true, email: true
        password blank: false, nullable: false, size: 6..255
    }

    static mapping = {
        password column: '`password`'
    }

    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    /* user details */
    String firstName;
    String lastName;

    Set<Role> getAuthorities() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}

提前致谢

Thanks in advance

推荐答案

您可以使用Groovy的元编程来覆盖encodePassword方法,如果您只想确保User对象已保存,并且您不关心测试的密码。

You can use Groovy's meta-programming to override the encodePassword method if you only want to make sure the User object is saved and you don't care about the password for your test.

@TestFor(UserService)    
@Mock(User)
class UserServiceTest {
    void testMethod() {
        User.metaClass.encodePassword = { -> }

        service.invokeTestThatSavesUserDomainClass()
    }   
}

这篇关于null springSecurityService会导致encodePassword在Grails 2.0.1中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 00:40