【问题标题】:Change password form which validates old password with spring security更改密码表单,使用 Spring Security 验证旧密码
【发布时间】:2013-03-21 02:34:11
【问题描述】:

我使用 grails 进行 Web 开发,使用 Spring Security 进行身份验证和授权。

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

问题是我无法找出正确的方法来验证旧密码与当前密码。我考虑过使用springSecurityService.encodePassword 函数并比较哈希值来手动完成。但我不确定这是否是正确的做法。

此表单仅供已经登录的用户访问。如果攻击者以某种方式控制了会话(例如用户忘记注销),则询问密码应该可以阻止攻击者更改密码

有没有一种弹簧安全方法可以做到这一点?

【问题讨论】:

  • 使用springSecurityService.encodePassword 看起来不错

标签: grails spring-security


【解决方案1】:

Spring Security Core 文档中有an example 使用passwordEnconder,但springSecurityService.encodePassword 也可以。

【讨论】:

  • 我已经测试了这两种方法,它们都可以完美运行。我坚持使用密码编码器,因为我认为这更干净。
  • 看起来列出的文档链接不再有效,但我认为这是等效页面:grails-plugins.github.io/grails-spring-security-core/guide/…
  • @Michael 你提供的链接也失效了
【解决方案2】:

这是我在 Grails 2.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...
      }
   }

}

【讨论】:

    【解决方案3】:

    这就是它为我服务的方式,我验证了新密码的确认。

    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
        }
    }`
    

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 1970-01-01
      • 2017-04-16
      • 2011-07-26
      • 2012-09-12
      • 2013-11-03
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多