SpringBoot整合BCrypt实现密码加密
作者:小郑要做干饭人 时间:2021-12-09 20:01:08
本文实例为大家分享了SpringBoot整合BCrypt实现密码加密的具体代码,供大家参考,具体内容如下
一. 首先在pom依赖中加入依赖:
<!-- security依赖包 (加密) -->
<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;
//获得BCrypt支持
@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;
/**
* @date: 2021/11/25/ 14:30
* @author: ZhengZiXuan
* @title: 使用BCrypt进行密码加密
* @description: 引入Security依赖默认开启了登录校验,访问API会跳转到登录页,如果只是需要BCrypt加密功能可以在启动类配置@SpringBootApplication (exclude = { org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class })禁用Security相关功能。
*/
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);
//返回true
System.out.println("相同代码对比: "+same_password_result);
boolean other_password_result = bCryptPasswordEncoder.matches("1234456",newPassword);
//返回false
System.out.println("其他密码对比: " + other_password_result);
}
}
运行结果如下:
来源:https://blog.csdn.net/a1422655169/article/details/121544107
标签:SpringBoot,BCrypt,加密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java流程控制之循环结构for,增强for循环
2023-11-03 10:45:20
![](https://img.aspxhome.com/file/2023/4/58834_0s.jpg)
MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)
2023-11-29 05:02:37
Android SharedPreferences存储用法详解
2023-08-07 08:25:34
![](https://img.aspxhome.com/file/2023/8/83528_0s.gif)
SpringMVC框架post提交数据库出现乱码解决方案
2022-03-01 09:50:41
![](https://img.aspxhome.com/file/2023/1/61421_0s.png)
jar包手动添加到本地maven仓库的步骤详解
2023-11-23 05:09:37
![](https://img.aspxhome.com/file/2023/5/59785_0s.png)
深入了解JAVA HASHMAP的死循环
2023-11-28 00:32:12
Java Stopwatch类,性能与时间计时器案例详解
2023-07-24 04:08:50
详解Spring Boot Profiles 配置和使用
2021-10-05 22:54:57
Windows下RabbitMQ安装及配置详解
2022-04-29 08:11:18
![](https://img.aspxhome.com/file/2023/1/62411_0s.png)
关于MyBatis模糊查询的几种实现方式
2023-05-09 04:23:12
java使用dom4j操作xml示例代码
2022-03-21 18:28:38
SpringBoot使用AOP+注解实现简单的权限验证的方法
2022-07-29 00:59:09
IDEA实现添加 前进后退 到工具栏的操作
2021-08-30 21:34:48
![](https://img.aspxhome.com/file/2023/7/62687_0s.jpg)
Java 方法签名详解及实例代码
2022-02-04 05:56:06
Java的接口和抽象类深入理解
2023-01-26 02:19:22
SpringBoot Security安装配置及Thymeleaf整合
2023-11-27 16:18:41
在IDEA里gradle配置和使用的方法步骤
2023-11-23 16:07:58
![](https://img.aspxhome.com/file/2023/1/59851_0s.png)
spring boot整合Shiro实现单点登录的示例代码
2023-04-07 01:17:56
![](https://img.aspxhome.com/file/2023/5/65145_0s.png)
java中this与super关键字的使用方法
2022-05-04 22:03:29
浅谈Java中向上造型向下造型和接口回调中的问题
2023-11-09 13:51:46
![](https://img.aspxhome.com/file/2023/1/59151_0s.jpg)