【问题标题】:Grails Spring-Security -how to compare passwords-Grails Spring-Security - 如何比较密码-
【发布时间】:2014-07-29 17:46:02
【问题描述】:

我正在使用 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

(好像会有盐 - 但我自己没有配置)

我的设置或多或少是默认设置;除了三个域类的包名。

由于文档自严重几天以来一直处于关闭状态,我在这里询问是否有人知道我做错了什么......

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    试试这个

    def passwordEncoder
    ...
    passwordEncoder.isPasswordValid(user.password, params.currentPassword, null)
    

    更多详情请参阅this post

    【讨论】:

    • 谢谢!你知道为什么params.currentPassword == springSecurityService.encodePassword(password) 不起作用吗?
    • 它对我不起作用。我正在使用 Grails 3.3.0 和 spring security core :3.2.0.M1 。在数据库 h2 中,我看到密码以 $2 开头,我认为这不是获取纯密码的可逆过程。无论如何,这个方法 passwordEncoder.isPasswordValid(user.password, params.currentPassword, null) 没有帮助。
    【解决方案2】:
    def springSecurityService
    if(springSecurityService?.passwordEncoder?.matches(currentPassword , 
    currentUser.password )){
     println("password matched")
    }
    

    鉴于: currentPassword = 原始/未编码密码

    currentUser.password = 编码密码

    【讨论】:

      【解决方案3】:

      使用 out 的另一种方法是:

      
      new BCryptPasswordEncoder().matches(plainPassword,encodedUserPassword);
      
      

      其中普通密码是原始密码值,编码密码是springSecurityService散列的密码

      【讨论】:

        【解决方案4】:
        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")
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-19
          • 2012-04-10
          • 2013-11-21
          • 2012-09-12
          • 2021-10-22
          • 2016-01-01
          • 2015-10-01
          • 2015-01-08
          相关资源
          最近更新 更多