1.BCryptPasswordEncoder使用之前要加入依赖
如果是SSM加入的依赖
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
如果是SpringBoot加入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐security</artifactId>
</dependency>
BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder(); 加密: bcryptPasswordEncoder.encode(password); //password是输入的密码,encodedPassword是通过bcryptPasswordEncoder进行加密的密码 解密: bcrytPasswordEncoder.matches(password,encodedPassword)
测试:
package com.qingfeng.service.impl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class Test {
public static void main(String[] args) {
String password = "123456";
BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
//加密:bcryptPasswordEncoder进行密码加密
String encodedPassword = bcryptPasswordEncoder.encode(password);
System.out.println("bcryptPasswordEncoder进行密码加密:"+encodedPassword);
//解密:
boolean flag = bcryptPasswordEncoder.matches(password, encodedPassword);
//如果flag为true,则解密成功 false,则解密失败
System.out.println("解密:"+flag);
}
}
测试结果:
bcryptPasswordEncoder进行密码加密:$2a$10$z1l7KwMFGthgsNOg6h0I4OVTUUyhC11paX1PN8glw7bT3tL4feZ1u 解密:true
Spring security中的BCryptPasswordEncoder方法对密码进行加密与密码匹配
原文:https://www.cnblogs.com/Amywangqing/p/13640838.html