2012-12-21 28 views
1

我在此Grails项目上使用Spring Security核心。我得到的错误“密码”无法在BootStrap类中解决。无法解析Grails项目中的符号

我有这个领域类:

class Person { 

transient springSecurityService 

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

static constraints = { 
    username blank: false, unique: true 
    password blank: false 
} 

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

Set<Authority> getAuthorities() { 
    PersonAuthority.findAllByPerson(this).collect { it.authority } as Set 
} 

def beforeInsert() { 
    encodePassword() 
} 

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

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

,这是我BootsStrap类:

class BootStrap { 




def init = { servletContext -> 

    if (!Person.count()) { 
     createData() 
    } 
} 
def destroy = { 
} 

private void createData() { 
    def userRole = new Authority(authority: 'ROLE_USER').save() 



    [harry: 'Harry Brock'].each { userName, realName -> 
     def user = new Person(username: userName, realName: realName, password: password, enabled: true).save() 
     PersonAuthority.create user, userRole, true 
    } 
} 
} 

我使用Grails 2.2和Spring Security的核心1.2.7.3

回答

2

在自举你正在使用名为password的未定义变量。

我说有问题的线以上评论:

String password  //apparently it missed, but the other 3 are also needed 
boolean accountExpired 
boolean accountLocked 
boolean passwordExpired 

所以保存userInstance是这样的:当你实例化一个User对象

[harry: 'Harry Brock'].each { userName, realName -> 
    // userName and realName are closure parameters, enabled is always true.. but where is password defined? 
    def user = new Person(username: userName, realName: realName, password: password, enabled: true).save() 
    PersonAuthority.create user, userRole, true 
} 
+0

谢谢。我正在尝试将密码设置为密码。我这样做了:password:“password” –

0

所有这些属性不能为空:

def user = new Person(username: userName, realName: realName, password: password, enabled: true, accountExpired:false, accountLocked:false, passwordExpired:false).save() 

或者,您可以在beforeInsert()中放置布尔属性来简化您r代码:

def beforeInsert() { 
    enabled=true 
    accountExpired=false 
    accountLocked=false 
    passwordExpired=false 
    encodePassword() 
}