使用背景:新开一个H5的项目,需要登录后暂时个人信息.这里只需要验证用户名密码是否正确,所有只是引用了BCryptPasswordEncoder类进行密码匹配.(原项目后台是用SpringSecurity进行的加密,没有盐)
<!--security-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
或者
<!--Spring Security的核心是spring-security-core, 它包含认证和访问权限控制功能-->
<!--我用的是这个依赖-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
// 测试比较明文明密码与数据库密码,项目中根据实际需求修改
public class passwordEncoderUtil {
public static void main(String[] args) {
// 加密 同一个明文加密两次,却输出了不同的结果,但是匹配匹配结果仍为true
System.out.println(new BCryptPasswordEncoder().encode("123456"));
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String encode1 = bCryptPasswordEncoder.encode("123456");
System.out.println("加密密码:" + encode1);
// 比较明文密码与数据库的密码
boolean matches1 = bCryptPasswordEncoder.matches("123456", "$2a$10$ZLTmLN24fg36gBe6t3NoMOW1BreOi4PfbZdy3lj5JDx01MpvkBz2a");
System.out.println("比较密码:" + matches1);
}
}