2011-06-28 72 views
0

我在Grails應用中使用Spring安全核心插件。我設法安裝插件,並在bootstrap中提供了一個默認的用戶,它工作正常。不過,我想提供在應用程序中創建新用戶(註冊)的選項。什麼是完成這個最好的方法?我試圖在UserController中下面的代碼:用Grails的彈簧安全插件創建用戶

def save = { 
    def userRole = SecRole.findByAuthority("ROLE_USER")?: new SecRole(authority:"ROLE_USER").save(failOnError:true) 
    def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority:"ROLE_ADMIN").save(failOnError:true) 

    def newUser = new User(
         username: params.username, 
         password: springSecurityService.encodePassword(params.password), 
         enabled: true) 

    SecUserSecRole.create newUser, userRole 
} 

,但它不工作,並拋出了顯示java.lang.NullPointerException。 有什麼建議嗎? 在此先感謝。

回答

2

您需要在用戶上撥打save()

+0

謝謝!我完全忘了save() –

0

我試圖加載不存在的角色。我用上面的註釋測試了它(保存用戶,修復我的錯誤並且兩個版本都可以工作後不保存)。所以SecUserSecRole.create對我來說已經足夠了。

相關問題