通过BCryptPasswordEncoder的加密的相同字符串的结果是不同的,如果需要判断是否是原来的密码,需要用它自带的方法。
加密:
BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
encode.encode(password);
判断:
需要通过自带的方法 matches 将未经过加密的密码和已经过加密的密码传进去进行判断,返回布尔值。
encode.matches(oldpassword,user1.getPassword());
举例说明:
public class BCryptPasswordEncoderTest {
public static void main(String[] args) {
String pass = "admin";
BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
String hashPass = bcryptPasswordEncoder.encode(pass);
System.out.println(hashPass);
boolean f = bcryptPasswordEncoder.matches("admin",hashPass);
System.out.println(f);
}
}
可以看到,每次输出的hashPass 都不一样,
但是最终的f都为 true,即匹配成功。
查看代码,可以看到,其实每次的随机盐,都保存在hashPass中。
在进行matchs进行比较时