2012-09-09 74 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()',它的依赖不注入。任何建议? –