在测试中断言SpringSecurity编码的密码

时间:2012-09-09 13:06:55

标签: grails spring-security encode

我想做类似的事情:

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 个答案:

答案 0 :(得分:1)

不要自己编码,请尝试使用

springSecurityService.encodePassword(user.password)

别忘了你的盐!如果你有一个(如果你没有,你应该考虑一个!)你可以将其作为第二个参数传递。

相关问题