【发布时间】:2020-09-21 02:26:13
【问题描述】:
我的密码在保存时在数据库中进行了哈希处理。但是我现在正在实现登录模块,并且无法在 spring [boot] data jpa 中比较两个密码。这是尝试将我输入的文本密码与在数据库中散列的密码进行比较,这显然失败了。
我知道这个问题已经回答here using java 用于一般情况,但不适用于弹簧靴。
我需要在这段代码中操作逻辑
@Override
public boolean getLoggedinUser(User user) {
String uname = user.getUsername();
String email = user.getEmail();
String password = user.getPassword();
System.out.println("sanity check username----- " + uname +
" password ------- " + password);
if ((userRepo.findByUsername(uname) != null && userRepo.findByPassword(password) != null)
|| (userRepo.findByPassword(password) != null && userRepo.findByEmail(email) != null)) {
return true;
}
else return false;
}
现在,如何比较它们?任何解决方案表示赞赏。
【问题讨论】:
-
我看到的唯一解决方案是将散列/编码机制移出数据库并将其应用于给定密码。这样您就可以比较哈希/编码密码。
-
你考虑过使用 BCrypt 吗? stackoverflow.com/questions/26905721/…
-
@Romano 是的,我正在使用 Bcrypt 。 BCryptPasswordEncoder bcryptEncoder = new BCryptPasswordEncoder(); boolean isMatched = bcryptEncoder.matches(password, userRepo.findByPassword(password)); System.out.println("匹配 ???? ----------------" + isMatched);返回 false 为什么?我输入的密码正确。
-
您是否 100% 确定也使用 BCrypt 生成了数据库密码?如果是,您是否使用盐?
-
@Romano,是的,它正在生成 100% ,但我无法比较它。
标签: java spring-boot spring-data-jpa