我正在使用SpringSecurity 2.0-RC2,并希望用户在联机时提供更改密码的可能性。
我的用户域类具有以下
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
要检查用户是否正在输入正确的当前密码,我在控制器中执行以下操作:
if (springSecurityService.encodePassword(params.currentPassword) == user.password) {
...但是(对我而言)这个检查总是失败。如果我这样做会更奇怪:
println springSecurityService.encodePassword(params.currentPassword)
println springSecurityService.encodePassword(params.currentPassword)
我在控制台中收到以下内容
$ 2A $ 10 $ sWt7mUSHPFT.Np6m.gXyl.h8tWqblJbwtzQ6EQeMHxXMoGwOffC3e $ 2A $ 10 $ lwHz1SkNlW8ibznt.mOiruAg5eG / BTtsjM7ChyYVBvamRcrL8tucm
(就像会有盐 - 但我自己没有配置)
我的设置或多或少是默认设置;除了三个域类的包名。
由于文件很严重,因为我在这里问到是否有人知道我做错了什么......
答案 0 :(得分:16)
试试这个
def passwordEncoder
...
passwordEncoder.isPasswordValid(user.password, params.currentPassword, null)
有关详细信息,请参阅this post。
答案 1 :(得分:0)
def springSecurityService
if(user.password == springSecurityService.encodePassword(params.currentPassword)){
println("User Password and params password is same")
} else {
println("User Password and params password are not equal")
}
答案 2 :(得分:0)
另一种方法是使用:
new BCryptPasswordEncoder().matches(plainPassword,encodedUserPassword);
其中普通密码是原始密码值,编码密码是由 springSecurityService 散列的密码