【问题标题】:Cannot resolve symbol in Grails project无法解析 Grails 项目中的符号
【发布时间】:2012-12-09 09:34:30
【问题描述】:

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

【问题讨论】:

    标签: grails groovy spring-security


    【解决方案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
    }
    

    【讨论】:

    • 谢谢。我试图将密码设置为密码。我是这样做的:密码:“密码”
    【解决方案2】:

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

    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-02-29
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      相关资源
      最近更新 更多