SpringBoot使用jasypt加解密密碼的實(shí)現(xiàn)方法
jasypt是一個(gè)通用的加解密庫,我們可以使用它在配置文件中對(duì)數(shù)據(jù)庫密碼進(jìn)行加密,以確保其安全性。
1、注入依賴
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
2、配置文件
#以數(shù)據(jù)庫密碼加密為例 ## 數(shù)據(jù)源配置 spring.datasource.url=jdbc:mysql://lochost:3306/jasypt?characterEncoding=utf8 spring.datasource.username=root #Fddt+VfcW5+j5lAbuOXxPB3mGb0iBLLe 是采用jasypt進(jìn)行加密以后生成的密文 spring.datasource.password=ENC(Fddt+VfcW5+j5lAbuOXxPB3mGb0iBLLe) spring.datasource.driver-class-name=com.mysql.jdbc.Driver #jasypt加密的密匙 jasypt.encryptor.password=abcderf(這個(gè)是自己設(shè)置的)
那么如何得到這個(gè)密文呢?
1、win+r cmd打開命令窗口 在你的maven庫中找到 jasypt-1.9.2.jar 包
執(zhí)行下面的命令
java -cp D:\Maven\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="密鑰(abcderf)" password=root(加密的密碼) algorithm=PBEWithMD5AndDES
然后復(fù)制密文即可

2、代碼生成(這種方法沒有使用過 參考鏈接:http://m.fzitv.net/article/197600.htm)
import org.jasypt.util.text.BasicTextEncryptor;
public class Test {
public static void main(String[] args) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(鹽)
textEncryptor.setPassword("PBEWithMD5AndDES");
//要加密的數(shù)據(jù)(數(shù)據(jù)庫的用戶名或密碼)
String username = textEncryptor.encrypt("root");
String password = textEncryptor.encrypt("root");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
}
到此這篇關(guān)于SpringBoot使用jasypt加解密密碼的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringBoot加解密密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例
這篇文章主要介紹了java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
SpringBoot使用SOFA-Lookout監(jiān)控的方法
本文介紹SpringBoot使用螞蟻金服SOFA-Lookout配合Prometheus進(jìn)行監(jiān)控,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解一)
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式,采用完全獨(dú)立于語言的文本格式,是理想的數(shù)據(jù)交換格式。接下來通過本文給大家介紹使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法,需要的朋友參考下吧2016-03-03
druid升級(jí)后sql監(jiān)控頁面為空白的解決
這篇文章主要介紹了druid升級(jí)后sql監(jiān)控頁面為空白的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
springboot從application.properties中注入list,?map方式
這篇文章主要介紹了springboot從application.properties中注入list,map方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

