2012-11-26 41 views
2

剛剛在Groovy中開始編程並已停滯不前。我正在嘗試創建可在我的引導程序中登錄的用戶,我已經看到了大量的教程,儘管複製和粘貼代碼讓我受到無數次錯誤的迎接。我現在已經開始關注似乎運行的代碼,但用戶根本就不在那裏。使用SpringSecurityCore引導程序

我在做什麼錯?

import grails.timesecurity.* 
import timetracker2.* 

class BootStrap { 
    def springSecurityService 

    def init = { servletContext -> 

     def examples = [ 
      'rob' : [username: 'rob', password: 'password'] 
     ] 

     def userRole = Authority.findByAuthority("ROLE_USER") ?: new Authority(authority: "ROLE_USER").save() 
     def adminRole = Authority.findByAuthority("ROLE_ADMIN") ?: new Authority(authority: "ROLE_ADMIN").save() 

     if(!Person.count()) 
     { 
      userRole = new Authority(authority: 'ROLE_USER').save() 

      //String password = springSecurityService.encodePassword('password') 

      def user = new Person(
       username: "Rob", 
       password: springSecurityService.encodePassword("Password"), 
       enabled: true 
       ) 
      PersonAuthority.create user, userRole 
      //def user = new Person['Rob', password, enabled: true].save() 
     }  
    } 

    def destroy = {} 
} 

任何人都可以幫忙的是一個傳奇!

回答

0

首先,我打賭你需要撥打save()在你的新Person。之後,確保您的Person對象正在驗證中。

4

您不要在Person實例上調用save()。一旦這個問題解決了,你將無法登錄,因爲你正在關注一箇舊的教程或博客文章,並且你明確地編碼了密碼。但生成的Person類已經這樣做了,所以它將被雙重編碼。而爲了進一步混淆事情,你正在用ROLE_USER創建第二個權威。

試試這個:

def userRole = Authority.findByAuthority("ROLE_USER") ?: new Authority(authority: "ROLE_USER").save() 
def adminRole = Authority.findByAuthority("ROLE_ADMIN") ?: new Authority(authority: "ROLE_ADMIN").save() 

if (!Person.count()) { 

    def user = new Person(
     username: "Rob", 
     password: "Password", 
     enabled: true).save() 

    PersonAuthority.create user, userRole 
} 
+0

你真棒,謝謝 – user1854751