grails新的安全插件给出空指针异常

时间:2011-08-23 22:37:25

标签: grails grails-plugin

我直接在安全核心插件的教程之后在Bootstrap.groovy中添加了以下代码,我从Bootstrap.groovy获得了一个空指针异常。知道什么是错的吗?

谢谢,Ray

P.S。我使用推荐的插件推荐的s2-quickstart命令添加了User和Role类,该命令生成User,Role和UserRole域类。下一步是在Bootstrap中设置一些示例用户,这是失败的。


BootStrap.groovy中:

       println "Creating roles user, leader, and admin"
       def userRole = Role.findByAuthority("ROLE_USER") ?: new Role(authority: "ROLE_USER").save()
       def leaderRole = Role.findByAuthority("ROLE_LEADER") ?: new Role(authority: "ROLE_LEADER").save()
       def adminRole = Role.findByAuthority("ROLE_ADMIN") ?: new Role(authority: "ROLE_ADMIN").save()

       println "Creating users called user, leader, and admin"
       def user = new User(username: "user", password: springSecurityService.encodePassword("abc"), enabled: true)
       def leader = new User(username: "leader", password: springSecurityService.encodePassword("abc"), enabled: true)
       def admin = new User(username: "admin", password: springSecurityService.encodePassword("abc"), enabled: true)

       println "Now joining users and their roles"
       UserRole.create(user, userRole)  <------------- FAILING HERE
       UserRole.create(leader, leaderRole)
       UserRole.create(admin, adminRole)
       println "All Done Creating users & roles"

产生以下NullPointerException

Creating roles user, leader, and admin

Creating users called user, leader, and admin

Now joining users and their roles

2011-08-23 14:34:10,762 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: null

java.lang.NullPointerException
    at $Proxy18.save(Unknown Source)
      **at momentum.UserRole.create(UserRole.groovy:32)**
    at momentum.UserRole.create(UserRole.groovy)
    at momentum.UserRole$create.call(Unknown Source)
    **at BootStrap$_closure1.doCall(BootStrap.groovy:168)**

线条如下:

BootStrap.groovy第168行: UserRole.create(user, userRole)

UserRole.groovy第32行: new UserRole(user: user, role: role).save(flush: flush, insert: true)

1 个答案:

答案 0 :(得分:2)

您永远不会保存新用户。

user.save()
leader.save()
admin.save()