2012-09-09 73 views
0

我想要做的事,如:測試斷言SpringSecurity編碼的密碼

class MySpec extends Specification{ 
    def 'test'(){ 
     given: 'a user that has its password encoded by SpringSecurity' 
      def user = new SecUser(username: 'blah', password: 'p').save(flush:true) 
      MessageDigest md = MessageDigest.getInstance('MD5') 
      md.update('p'.getBytes('UTF-8')) 
     expect: 'the password should be encoded with MD5 algorithm' 
      user.password == (new BASE64Encoder()).encode(md.digest()) 
    } 
} 

在我Config.groovy中添加以下行:

grails.plugins.springsecurity.password.algorithm =「MD5 「

這不起作用(斷言失敗)。有任何想法嗎??

回答

1

而是自己做編碼的,請嘗試使用

springSecurityService.encodePassword(user.password) 

不要忘記你的鹽!如果你有一個(如果你不這樣做,你應該考慮一個),你可以把它作爲第二個參數傳入。

+0

我不能在我的規範注入springSecurityService,如果我初始化'新的SpringSecurityService()',它的依賴不注入。任何建議? –