2013-03-30 140 views
3

Im使用grails进行Web开发和Spring安全性验证和授权。更改用弹簧安全验证旧密码的密码表

我想制作一个简单的表单让用户更改密码。这是一个包含三个密码字段的表单。第一个是为当前(旧)密码设定的。第二个和第三个是用于验证的新密码,以防止意外错误的输入。

问题是,我不能找出正确的方法来验证旧密码对当前的一个。我想通过使用springSecurityService.encodePassword函数并比较哈希来手动完成它。但我不确定这是否是正确的做法。

此表仅适用于已登录的用户访问。问计于密码应更改密码阻止攻击者,如果他们以某种方式得到了会议的举行(用户忘记注销例如)

有没有一种弹簧安全的方式来做到这一点?

+1

使用'springSecurityService.encodePassword'看起来不错 –

回答

3

在使用passwordEnconder的Spring Security Core文档中有an example,但springSecurityService.encodePassword也没问题。

+0

我测试了两种方法,并且它们完美地工作。我坚持使用密码编码器,因为我认为这样更干净。 – Sponiro

+2

看起来像列出的文档链接不再有效,但我认为这是等效页面:http://grails-plugins.github.io/grails-spring-security-core/guide/passwords.html#locking – Michael

+2

@迈克尔,你提供的链接也是死的 – rahulserver

1

这就是我在Grails 2.3和spring-security-core-2.0-RC4中使用的。

def springSecurityService 
User activeUser = springSecurityService.currentUser  
def password_old ='password' 
if (!springSecurityService.passwordEncoder.isPasswordValid(activeUser.getPassword(), password_old, null)) { 
"password is incorrect do something" 

}else log.info "password is correct do something"