SpringBoot整合BCrypt实现密码加密
作者:小郑要做干饭人 发布时间:2021-12-09 20:01:08
标签:SpringBoot,BCrypt,加密
本文实例为大家分享了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


猜你喜欢
- Java 异步实现的几种方式1. jdk1.8之前的Futurejdk并发包里的Future代表了未来的某个结果,当我们向线程池中提交任务的
- 背景两张表,分别是 :sys_tbl,和 sys_field,其中:sys_tbl 是系统所有表的信息,包含两个字段 :code(表名),n
- 本文实例讲述了Java自定义注解用法。分享给大家供大家参考,具体如下:一 自定义注解语法[public] @interface Annota
- String 不是简单类型,而是一个类,它被用来表示字符序列。字符本身符合 Unicode 标准,其初始化方式有两种。如:String gr
- 在微信公众号里面需要上传头像,时间比较紧,调用学习jssdk并使用 来不及 就用了input1、使用input:file标签, 去调用系统默
- public static string Escape(string s) &nb
- Maven是项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件。Maven是一个项目管理工具,它包
- 求数组中最大的数的值:1、数组的max函数:class Program { &nb
- Flutter底部和顶部导航栏的实现,供大家参考,具体内容如下带文字图标的底部导航栏(使用BottomNavigationBar和Botto
- 最近在做图片相关的应用,所以就各方积累到一些常用的操作,一般来说会有多种方式来实现这一功能,比如 1.采用色度变换 2.
- 什么是自旋锁说道自旋锁就要从多线程下的锁机制说起,由于在多处理器系统环境中有些资源因为其有限性,有时需要互斥访问(mutual exclus
- Jenkins 关闭和重启我们用jar -jar jenkins.war来启动jenkins服务器,那么我们如何关闭或者重启jenkins服
- 前言Spring 事务注解 @Transactional 本来可以保证原子性,如果事务内有报错的话,整个事务可以保证回滚,但是加上try c
- 一、简介编写手机App时,有时需要使用文字转语音(Text to Speech)的功能,比如开车时阅读收到的短信、导航语音提示、界面中比较重
- 打开idea项目后部分目录下出现橙色的时钟标志(如下):可以看到所有的java文件都显示了后缀名.java,文件的图标都变成了橙色的原因项目
- 一、LINQ的体系结构语言集成查询 (LINQ) (C#) | Microsoft 官方文档LINQ总共包括五个部分: 程序集命名
- 1、效果2、简介本文主角是ItemTouchHelper。它是RecyclerView对于item交互处理的一个「辅助类」,主要用于拖拽以及
- 本文实例为大家分享了Android仿京东分类效果展示的具体代码,供大家参考,具体内容如下1.写一个fragmentimport androi
- 今天看了看Java并发程序,写一写入门程序,并设置了线程的优先级。class Elem implements Runnable{  
- 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应, 同时我们又需要在工作线程中更新UI界面上的控件。但直接访