SpringBoot整合BCrypt實現(xiàn)密碼加密
更新時間:2021年11月26日 17:34:16 作者:小鄭要做干飯人
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合BCrypt進(jìn)行密碼加密,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了SpringBoot整合BCrypt實現(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>
二.在啟動類中加入@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)開啟了登錄校驗,訪問API會跳轉(zhuǎn)到登錄頁,如果只是需要BCrypt加密功能可以在啟動類配置@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("相同代碼對比: "+same_password_result); boolean other_password_result = bCryptPasswordEncoder.matches("1234456",newPassword); //返回false System.out.println("其他密碼對比: " + other_password_result); } }
運行結(jié)果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 解決Spring security5.5.7報錯Encoded password does not look like BCrypt異常
- 使用spring?security?BCryptPasswordEncoder接入系統(tǒng)
- 如何在spring boot項目中使用Spring Security的BCryptPasswordEncoder類進(jìn)行相同密碼不同密文的加密和驗證
- 一文掌握SpringSecurity?BCrypt密碼加密和解密
- Springboot基于BCrypt非對稱加密字符串的實現(xiàn)
- Spring security BCryptPasswordEncoder密碼驗證原理詳解
- Spring項目使用Maven和BCrypt實現(xiàn)修改密碼功能方式
相關(guān)文章
Kafka是什么及如何使用SpringBoot對接Kafka(最新推薦)
這篇文章主要介紹了Kafka是什么,以及如何使用SpringBoot對接Kafka,今天我們通過一個Demo講解了在SpringBoot中如何對接Kafka,也介紹了下關(guān)鍵類?KafkaTemplate,需要的朋友可以參考下2023-11-11Springboot使用@RefreshScope注解實現(xiàn)配置文件的動態(tài)加載
本文主要介紹了Springboot使用@RefreshScope注解實現(xiàn)配置文件的動態(tài)加載,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09使用spring boot開發(fā)時java對象和Json對象轉(zhuǎn)換的問題
這篇文章主要介紹了使用spring boot開發(fā)時java對象和Json對象轉(zhuǎn)換的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03基于Restful接口調(diào)用方法總結(jié)(超詳細(xì))
下面小編就為大家?guī)硪黄赗estful接口調(diào)用方法總結(jié)(超詳細(xì))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08maven打包web項目時同時打包為war和jar文件的方法
本篇文章主要介紹了maven打包web項目時同時打包為war和jar文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Java創(chuàng)建多線程局域網(wǎng)聊天室實例
這篇文章主要介紹了Java創(chuàng)建多線程局域網(wǎng)聊天室實例,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07