1.导入依赖
<!--Spring Security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.具体代码实现
@Test
void contextLoads() throws FileNotFoundException {
//new一个 BCryptPasswordEncoder
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//调用encode方法进行加密,传入我们需要加密的数据
String encode1 = passwordEncoder.encode("1234");
String encode2 = passwordEncoder.encode("1234");
System.out.println(encode1);
System.out.println(encode2);
//matches方法进行校验
boolean test1 = passwordEncoder.matches("1234", encode1);
boolean test2 = passwordEncoder.matches("1234", encode2);
System.out.println(test1);
System.out.println(test2);
}