Grails 更改使用spring security验证旧密码的密码表单
Im使用grails进行web开发,使用spring安全性进行身份验证和授权 我想制作一个简单的表单,允许用户更改密码。这是一个包含三个密码字段的表单。第一个应该是当前(旧)密码。第二个和第三个是用于新密码的验证,以防止意外键入错误 问题是,我无法找出正确的方法来验证旧密码与当前密码。我想通过使用Grails 更改使用spring security验证旧密码的密码表单,grails,spring-security,Grails,Spring Security,Im使用grails进行web开发,使用spring安全性进行身份验证和授权 我想制作一个简单的表单,允许用户更改密码。这是一个包含三个密码字段的表单。第一个应该是当前(旧)密码。第二个和第三个是用于新密码的验证,以防止意外键入错误 问题是,我无法找出正确的方法来验证旧密码与当前密码。我想通过使用springSecurityService.encodePassword函数并比较散列来手动完成。但我不确定这样做是否正确 此表单仅可供已登录的用户访问。如果攻击者以某种方式控制了会话(例如,用户忘记注
springSecurityService.encodePassword
函数并比较散列来手动完成。但我不确定这样做是否正确
此表单仅可供已登录的用户访问。如果攻击者以某种方式控制了会话(例如,用户忘记注销),则请求密码可以阻止他们更改密码
有没有spring security的方法可以做到这一点?spring security核心文档中有使用
PasswordEncoder
,但是springSecurityService.encodePassword
也可以 这就是我在grails2.3和spring-security-core-2.0-RC4中使用的内容
import com.example.User
import grails.plugin.springsecurity.SpringSecurityService
class UserController {
SpringSecurityService springSecurityService
def checkUserPasswordMatches(){
//Get current user
User user = springSecurityService.getCurrentUser()
String currentPassword = params.password
if (!springSecurityService.passwordEncoder.isPasswordValid(user.getPassword(), currentPassword, null)) {
log.info("INFO - Password does not match!"
//TODO: Do something now passwords match...
} else {
log.info("INFO - Password matches existing user password!"
//TODO: Do something after passwords mismatch...
}
}
}
这就是它为我服务的方式,我验证新密码的确认
def cambio= {
def respuesta = [error: 1]
SecUser user = springSecurityService.currentUser
if (params?.j_password && params?.password && params?.old_password) {
String oldPasword=params?.old_password
String newPassword = params.j_password
String newPassword2 = params.password
if (!springSecurityService.passwordEncoder.isPasswordValid(user.getPassword(), oldPasword, null)) {
respuesta.error=3
render respuesta as JSON
}else if(newPassword == newPassword2) {
user.setPassword(params.j_password)
user.save()
respuesta.error=0
render respuesta as JSON
}else
{
respuesta.error=2
render respuesta as JSON
}
}else{
render respuesta as JSON
}
}`
使用
springSecurityService.encodePassword
看起来不错,我已经测试了这两种方法,它们工作得非常好。我坚持使用密码编码器,因为我认为这更干净。看起来列出的文档链接不再有效,但我认为这是等效的页面:@Michael,你提供的链接也死了