一. 首先在pom依赖中加入依赖:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
二.在启动类中加入@EnableScheduling注解,获得BCrypt支持:
package com.zzx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三.模拟获得登录密码:
package com.zzx.test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class BCryptTest {
public static void main(String[] args) {
String password = "123456";
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String newPassword = bCryptPasswordEncoder.encode(password);
System.out.println("加密的密码为: "+newPassword);
boolean same_password_result = bCryptPasswordEncoder.matches(password,newPassword);
System.out.println("相同代码对比: "+same_password_result);
boolean other_password_result = bCryptPasswordEncoder.matches("1234456",newPassword);
System.out.println("其他密码对比: " + other_password_result);
}
}
运行结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/f3525f3030984cc6adfe3e97a5323a7d.png)