Grails登录不起作用

时间:2016-02-09 11:50:51

标签: spring grails spring-security

我正在使用带有弹簧安全性的Grails 2.4

       compile "org.grails.plugins:spring-security-core:2.0.0"

以下是我的bootstrap.groovy

   import com.aarestu.grailstest.Role
  import com.aarestu.grailstest.User
  import com.aarestu.grailstest.UserRole
   class BootStrap {

def init = { servletContext ->


    def user = new User(usernmae: 'user', password: 'user').save(flush: true, failOnError: true)
    def userSecond = new User(usernmae: 'user2', password: 'user').save(flush: true, failOnError: true)
    def admin = new User(usernmae: 'admin', password: 'admin').save(flush: true, failOnError: true)



    def userRole = new Role(authority: 'ROLE_USER').save(flush: true, failOnError: true)
    def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true, failOnError: true)

    new UserRole(user: user, role: userRole).save(flush: true, failOnError: true)
    new UserRole(user: admin, role: adminRole).save(flush: true, failOnError: true)

}
def destroy = {
}

}

它在表Role中创建了2个条目,但没有其他条目。 然后,我在数据库中手动创建了一个用户,当我尝试使用该用户登录时,即使我可以在数据库中查看它,它也无法正常工作。

如果

         failOnError:true

未包含然后它运行正常但我无法登录,如果包含在内我得到错误包含在最后

我还通过修改USER控制器以包含以下内容来解释双重编码

  boolean beforeInsertRunOnce = false
boolean beforeUpdateRunOnce = false
Set<Role> getAuthorities() {
    UserRole.findAllByUser(this)*.role
}
def beforeInsert() {
    if (! beforeInsertRunOnce) {
        beforeInsertRunOnce = true
        encodePassword()
    }
}
def afterInsert() {
    beforeInsertRunOnce = false
}

def beforeUpdate() {
    if (isDirty('password') && ! beforeUpdateRunOnce ) {
        beforeUpdateRunOnce = true
        encodePassword()
    }
}
def afterUpdate() {
    beforeUpdateRunOnce = false
}

错误

   Error |
2016-02-09 11:42:58,434 [localhost-startStop-1] ERROR  context.GrailsContextLoaderListener  - Error initializing the application:   Validation Error(s) occurred during save():
      - Field error in object 'com.aarestu.grailstest.User' on field 'username': rejected value [null]; codes    [com.aarestu.grailstest.User.username.nullable.error.com.aarestu.grailstest.User.username,com.aarestu.grailstest.User.username.nullable.error.username,com   .aarestu.grailstest.User.username.nullable.error.java.lang.String,com.aarestu .grailstest.User.username.nullable.error,user.username.nullable.error.com.aar estu.grailstest.User.username,user.username.nullable.error.username,user.user name.nullable.error.java.lang.String,user.username.nullable.error,com.aarestu .grailstest.User.username.nullable.com.aarestu.grailstest.User.username,com.aarestu.grailstest.User.username.nullable.username,com.aarestu.grailstest.User.username.nullable.java.lang.String,com.aarestu.grailstest.User.username.nullable,user.username.nullable.com.aarestu.grailstest.User.username,user.username.nullable.username,user.username.nullable.java.lang.String,user.username.nullable,nullable.com.aarestu.grailstest.User.username,nullable.username,nullable.java.lang.String,nullable]; arguments [username,class com.aarestu.grailstest.User]; default message [Property [{0}] of class [{1}] cannot be null]
Message: Validation Error(s) occurred during save():
- Field error in object 'com.aarestu.grailstest.User' on field 'username': rejected value [null]; codes  [com.aarestu.grailstest.User.username.nullable.error.com.aarestu.grailstest.User.username,com.aarestu.grailstest.User.username.nullable.error.username,com  .aarestu.grailstest.User.username.nullable.error.java.lang.String,com.aarestu .grailstest.User.username.nullable.error,user.username.nullable.error.com.aarestu.grailstest.User.username,user.username.nullable.error.username,user.username.nullable.error.java.lang.String,user.username.nullable.error,com.aarestu .grailstest.User.username.nullable.com.aarestu.grailstest.User.username,com.aarestu.grailstest.User.username.nullable.username,com.aarestu.grailstest.User .username.nullable.java.lang.String,com.aarestu.grailstest.User.username.nullable,user.username.nullable.com.aarestu.grailstest.User.username,user.username.nullable.username,user.username.nullable.java.lang.String,user.username.nullable,nullable.com.aarestu.grailstest.User.username,nullable.username,nullable.java.lang.String,nullable]; arguments [username,class com.aarestu.grailstest.User]; default message [Property [{0}] of class [{1}]  cannot be null]
 Line | Method
  ->>    9 | doCall                           in BootStrap$_closure1
 |    327 | evaluateEnvironmentSpecificBlock in grails.util.Environment
 |     320 | executeForEnvironment . . . . .  in     ''
 |    296 | executeForCurrentEnvironment     in     ''
 |    266 | run . . . . . . . . . . . . . .  in     java.util.concurrent.FutureTask
 |   1142 | runWorker                        in java.util.concurrent.ThreadPoolExecutor
 |    617 | run . . . . . . . . . . . . . .  in    java.util.concurrent.ThreadPoolExecutor$Worker
 ^    745 | run                              in java.lang.Thread
 Error |
 Forked Grails VM exited with error

非常感谢任何帮助或指向正确的方向

1 个答案:

答案 0 :(得分:0)

bootsrap.groovy代码错误

以下内容需要从

更改
       def user = new User(usernmae: 'user', password: 'user').save(flush: true, failOnError: true)
        def admin = new User(usernmae: 'admin', password: 'admin').save(flush: true, failOnError: true)
   Role(user: user, role: userRole).save(flush: true, failOnError: true)
    new UserRole(user: admin, role: adminRole).save(flush: true, failOnError: true)

         def user = new User(username: 'user', password: 'password', enabled: true).save()
        def admin = new User(username: 'admin', password: 'password', enabled: true).save()
        UserRole.create user, roleUser
        UserRole.create admin, roleUser
        UserRole.create admin, roleAdmin, true