Springboot基于BCrypt非對稱加密字符串的實現(xiàn)
1 : BCrypt簡介
在用戶模塊中,需要對于用戶的密碼進行保護,通常都會進行加密。
我們通常對密碼進行加密,然后存放在數(shù)據(jù)庫中,在用戶進行登錄的時候,將其輸入的密碼進行加密然后與數(shù)據(jù)庫中存放的密文進行比較,以驗證用戶密碼是否正確。
目前,MD5和BCrypt比較流行。相對來說,BCrypt比MD5更安全。
BCrypt官網(wǎng)地址:http://www.mindrot.org/projects/jBCrypt/
2 : 集成BCrypt加密及驗證
2.1 : 引入POM
<dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.3m</version> </dependency>
2.2 : 工具類
PassWordUtil.java
package com.utils; import org.mindrot.jbcrypt.BCrypt; public class PassWordUtil { ? ? /** ? ? ?* 密碼加密 ? ? ?*/ ? ? public static String encrypt(String source){ ? ? ? ? String salt = BCrypt.gensalt(); ? ? ? ? return BCrypt.hashpw(source, salt); ? ? } ? ? /** ? ? ?* 密碼校驗 ? ? ?*/ ? ? public static boolean check(String source, String pwdCode){ ? ? ? ? return BCrypt.checkpw(source, pwdCode); ? ? } }
2.3 : 驗證
public static void main(String[] args) { String password = "abc123&%*"; String crypt = encrypt(password); System.out.println(crypt); System.out.println("=========="); System.out.println(check(password, crypt)); System.out.println(check(password + "1", crypt)); }
到此這篇關(guān)于Springboot基于BCrypt非對稱加密字符串的實現(xiàn)的文章就介紹到這了,更多相關(guān)Springboot BCrypt非對稱加密字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決Spring security5.5.7報錯Encoded password does not look like BCrypt異常
- 使用spring?security?BCryptPasswordEncoder接入系統(tǒng)
- 如何在spring boot項目中使用Spring Security的BCryptPasswordEncoder類進行相同密碼不同密文的加密和驗證
- 一文掌握SpringSecurity?BCrypt密碼加密和解密
- SpringBoot整合BCrypt實現(xiàn)密碼加密
- Spring security BCryptPasswordEncoder密碼驗證原理詳解
- Spring項目使用Maven和BCrypt實現(xiàn)修改密碼功能方式
相關(guān)文章
Java8中關(guān)于Function.identity()的使用
這篇文章主要介紹了Java8中關(guān)于Function.identity()的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05KotlinScript構(gòu)建SpringBootStarter保姆級教程
這篇文章主要為大家介紹了KotlinScript構(gòu)建SpringBootStarter的保姆級教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09spring boot springMVC擴展配置實現(xiàn)解析
這篇文章主要介紹了spring boot springMVC擴展配置實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08SpringBoot Jpa企業(yè)開發(fā)示例詳細講解
這篇文章主要介紹了SpringBoot Jpa企業(yè)開發(fā)示例,Jpa可以通過實體類生成數(shù)據(jù)庫的表,同時自帶很多增刪改查方法,大部分sql語句不需要我們自己寫,配置完成后直接調(diào)用方法即可,很方便2022-11-11