无法获得Bcrypt.checkpw在Java Play框架中验证密码

问题描述 投票:-1回答:1

几年前,我编写了一个应用程序,并升级了一些罐子,但没有升级bcrypt

它是使用play框架以Java编写的。

BCrypt是从“ org.mindrot”%“ jbcrypt”%“ 0.3m”提供的]

我使用BCrypt.checkpw(password.trim(),user.getPassword())

其中密码是捕获的明文,而user.getPassword是在MySQL中存储为char(60)的哈希。

我使用BCrypt.hashpw(password.trim(),BCrypt.gensalt(15))哈希密码

checkpw使用原始密码很有意思,但是任何新密码都会失败,并且始终会返回false

一个有趣的发现是我在开发数据库中有一个密码,我知道它是“密码”其哈希看起来像这样

$ 2a $ 10 $ UvKgjjT。/ SuMlD6gsoyD0e2lBcOwFtL / mfGmneTou / lrU1R / ZwMLK

vs

我刚创建的新密码,并将其密码设置为'password'

其哈希看起来像这样

$ 2a $ 10 $ rNJzD52 / muHMkBF1Co9XF.VkQNRHQ3HCW.DYzke7jnY424voZwyq6

我知道它们应该有所不同,但格式看起来有所不同?

请提供任何帮助,因为这没有任何意义,因为没有任何变化,但是新用户无法注册

代码:密码在我的用户类中设置

public void setPassword(String password) {
        play.Logger.debug("setPassword |" + password.trim() +"|");
        this.password = BCrypt.hashpw(password.trim(), BCrypt.gensalt(15));
}

我在我的register方法中称呼它

public Result registeruser() {
        JsonNode json = request().body().asJson();
.
.
.
        if (json.has("password")) {
          user.setPassword(json.findPath("password").textValue())
        }
.
.
.
       user.save()
.
.
.
}

然后有以下身份验证方法

public static Users authenticate(String email, String password) {
        play.Logger.debug("email is " + email);
        play.Logger.debug("authenticate password entered |" + password.trim() +"|");

        Users user = Users.find.query().where().eq("email", email).findOne();
        if (user != null) {
            play.Logger.debug("password hash from db |" + user.getPassword() +"|");
            Boolean checkPassword = BCrypt.checkpw(password.trim(), user.getPassword());
            play.Logger.debug("checkPassword " + checkPassword);
            if (checkPassword) {               
                    return user;
            } else {
                    return null;
            }
       }
}

正在运行的相关调试输出

In setPassword part
[debug] application - setPassword |password|

in authenticate part
[debug] application - authenticate password entered |password|
[debug] application - password hash from db |$2a$10$EiuMUWfbCoO.A1GxKk9YeOhqtK0bn4O8Y/W9U/7bEN/CSObOm6jUa|
[debug] application - checkPassword false
java bcrypt playframework-2.3
1个回答
0
投票

原因是在setPassword中不是进行哈希处理的正确位置,我一直得到空白的hashpw。这曾经在早期版本的游戏中有效,但现在显然不再适用

我将其移动到@PrePersist方法,如下所示:

@PrePersist
    public void hashPassword(){
        play.Logger.debug("hashPassword |" + this.password +"|");
        String hashed = BCrypt.hashpw(this.password, BCrypt.gensalt(15));
        play.Logger.debug("hashed " + hashed);
        this.password = hashed;
    }

问题已解决

© www.soinside.com 2019 - 2024. All rights reserved.