创建账号时使用的是 new BCryptPasswordEncoder().encode(password)加密了密码
解密时同样使用BCryptPasswordEncoder
public int updateAccountPassword(Account account, String newPassword) {
// 查询旧密码
Account oldaccount = accountMapper.selectByPrimaryKey(account.getStuUsername());
// encode.matches比对密码是否相等
BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
if (encode.matches(account.getStuPassword(), oldaccount.getStuPassword())) {
String bcryptPasswd = (encode.encode(newPassword));
oldaccount.setStuPassword(bcryptPasswd);
// 更新新密码
return accountMapper.updateByPrimaryKey(oldaccount);
}
return -1;
}
encode.matches(明文:输入的用来确认的旧密码, 密文:查出来的数据库中的旧密码)
括号中的参数如果写反了会报 Encoded password does not look like BCrypt 错误