簡體   English   中英

使用 Grails 的 spring 安全插件創建用戶

[英]create users with spring security plug-in for Grails

我在 Grails 應用程序中使用 spring 安全核心插件。 我設法安裝了插件並在引導程序中提供了一個運行正常的默認用戶。 但是我想提供在應用程序中創建新用戶(注冊)的選項。 實現這一目標的最佳方法是什么? 我在 UserController 中嘗試了以下代碼:

 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。 有任何想法嗎? 提前致謝。

您需要在用戶上調用save()

我試圖加載不存在的角色。 我用上面的評論測試了它(在修復我的錯誤並且兩個版本都有效之后保存用戶而不是保存)。 所以 SecUserSecRole.create 對我來說就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM