1

我在这个 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

4

2 回答 2

1

在 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
}
于 2012-12-21T19:55:19.137 回答
0

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

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()
}
于 2012-12-22T04:10:24.683 回答