无法解析Grails项目中的符号

时间:2012-12-21 19:51:14

标签: grails groovy spring-security

我在这个Grails项目中使用Spring Security核心。我得到错误“密码”无法在BootStrap类中解析。

我有这个域类:

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 Core 1.2.7.3

2 个答案:

答案 0 :(得分:2)

在BootStrap中,您使用的是名为password的未定义变量。

我在问题的上面添加了评论:

[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
}

答案 1 :(得分:0)

实例化User对象时,所有这些属性都不能为null:

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

所以请像这样保存userInstance:

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

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

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