SpringBoot整合BCrypt實(shí)現(xiàn)密碼加密
本文實(shí)例為大家分享了SpringBoot整合BCrypt實(shí)現(xiàn)密碼加密的具體代碼,供大家參考,具體內(nèi)容如下
一. 首先在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>
二.在啟動(dòng)類中加入@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進(jìn)行密碼加密
* @description: 引入Security依賴默認(rèn)開(kāi)啟了登錄校驗(yàn),訪問(wèn)API會(huì)跳轉(zhuǎn)到登錄頁(yè),如果只是需要BCrypt加密功能可以在啟動(dòng)類配置@SpringBootApplication (exclude = { org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class })禁用Security相關(guān)功能。
*/
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("相同代碼對(duì)比: "+same_password_result);
boolean other_password_result = bCryptPasswordEncoder.matches("1234456",newPassword);
//返回false
System.out.println("其他密碼對(duì)比: " + other_password_result);
}
}
運(yùn)行結(jié)果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決Spring security5.5.7報(bào)錯(cuò)Encoded password does not look like BCrypt異常
- 使用spring?security?BCryptPasswordEncoder接入系統(tǒng)
- 如何在spring boot項(xiàng)目中使用Spring Security的BCryptPasswordEncoder類進(jìn)行相同密碼不同密文的加密和驗(yàn)證
- 一文掌握SpringSecurity?BCrypt密碼加密和解密
- Springboot基于BCrypt非對(duì)稱加密字符串的實(shí)現(xiàn)
- Spring security BCryptPasswordEncoder密碼驗(yàn)證原理詳解
- Spring項(xiàng)目使用Maven和BCrypt實(shí)現(xiàn)修改密碼功能方式
相關(guān)文章
Spring AOP 對(duì)象內(nèi)部方法間的嵌套調(diào)用方式
這篇文章主要介紹了Spring AOP 對(duì)象內(nèi)部方法間的嵌套調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊(duì)列方案及使用場(chǎng)景
在很多的業(yè)務(wù)場(chǎng)景中,延時(shí)隊(duì)列可以實(shí)現(xiàn)很多功能,此類業(yè)務(wù)中,一般上是非實(shí)時(shí)的,需要延遲處理的,需要進(jìn)行重試補(bǔ)償?shù)?這篇文章主要介紹了SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊(duì)列方案及使用場(chǎng)景,需要的朋友可以參考下2024-04-04
Java System類詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
System類是jdk提供的一個(gè)工具類,有final修飾,不可繼承,由名字可以看出來(lái),其中的操作多數(shù)和系統(tǒng)相關(guān)。這篇文章主要介紹了Java System類詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-04-04
解決myBatis中openSession()自動(dòng)提交的問(wèn)題
在學(xué)習(xí)MySQL過(guò)程中,發(fā)現(xiàn)插入操作自動(dòng)提交,問(wèn)題原因可能是myBatis中的openSession()方法設(shè)置了自動(dòng)提交,或者是MySQL的默認(rèn)引擎設(shè)置為不支持事務(wù)的MyISAM,解決辦法包括更改myBatis的提交設(shè)置或?qū)ySQL表的引擎改為InnoDB2024-09-09
SpringBoot集成Kafka的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成Kafka的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01

