2012-11-15 66 views
0

我已經注意到,很多的教程,我下面使用本:Grails的2.1.1和Spring Security的核心插件

def springSecurityService 

,因爲我想只有當前得到記錄用戶登錄我使用:

def user = params.id ? User.findByUserId(params.id) : User.get(springSecurityService.principal.id) 

,並在我的引導,我想創建一個用戶名和密碼,所以例如

def user = new User( username: username, password: springSecurityService.encodePassword("tops3kr17"), enabled: true)

但是我注意到密碼沒有被創建,並且Spring Source Tools沒有找到方法.principal.id或.encodePassword(它們在STS中保留下劃線),並且希望在使用CTL +時使用SpringSecurityService和大寫字母S。 SPACE(並且不會完成.principal.id或.encodePassword)。

,所以我有點失落,因爲它似乎教程過時

的那麼我該怎麼做我當前支持的方法是什麼描述?或者我錯過了一些非常簡單的事情? :)

class BootStrap { 
def springSecurityService 

def init = { servletContext -> 

    def demo = [ 
     'jack' : [ fullName: 'Jack Demo Salesman'], 
     'jill' : [ fullName: 'Jill Demo Saleswoman']] 

    def now = new Date() 
    def random = new Random() 

    def userRole = SecRole.findByAuthority("ROLE_SALES") ?: new SecRole(authority: "ROLE_SALES").save() 
    def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority: "ROLE_ADMIN").save() 

    def users = User.list() ?: [] 
    if (!users) { 
     demo.each { username, password, userAttrs -> 
      def user = new User(
       username: username, 
       password: springSecurityService.encodePassword('secret'), 
       enabled: true) 
      if (user.validate()) { 
       println "DEBUG: Creating user ${username}..." 
       println "DEBUG: and their password is ${password}" 
       user.save(flush:true) 

       SecUserSecRole.create user, userRole 
       users << user 
      } 
      else { 
       println("\n\n\nError in account bootstrap for ${username}!\n\n\n") 
       user.errors.each {err -> 
        println err 
       } 
      } 

回答

0

使用注入的SpringSecurityService實例是正確的方法。

def springSecurityService 

def foo() { 
    springSecurityService.principal 
    springSecurityService.encodePassword('fdsfads') 
    .... 
} 

如果IDE沒有識別出它,IDE就會出現問題。

+0

謝謝我會給它一個 – Basics

+0

搞明白了。我把代碼放在我原來的帖子中。至少我覺得我是這樣做的 。 :P似乎有一個問題得到修復找到另一個問題。 :) – Basics

相關問題