本篇內(nèi)容介紹了“Jasypt怎么對SpringBoot配置文件加密”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
引入jasypt
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version></dependency>
生成要加密的字符串
將數(shù)據(jù)庫的用戶名和密碼進行加密
public static void main(String[] args) { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //加密所需的salt(鹽) textEncryptor.setPassword("G0CvDz7oJn6"); //要加密的數(shù)據(jù)(數(shù)據(jù)庫的用戶名或密碼) String username = textEncryptor.encrypt("root"); String password = textEncryptor.encrypt("root123"); System.out.println("username:"+username); System.out.println("password:"+password); }
輸出信息為:
username:i8QgEN4uOy2E1rHzrpSTYA==password:6eaMh/RX5oXUVca9ignvtg==
或者使用Maven下載好的jar包加密\Maven\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=G0CvDz7oJn6 algorithm=PBEWithMD5AndDES input=root
輸出信息為:
----ENVIRONMENT-----------------Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11----ARGUMENTS-------------------input: rootalgorithm: PBEWithMD5AndDESpassword: G0CvDz7oJn6----OUTPUT----------------------Gvkoz+sbFWiRe3ECtizV1A==
拷貝-OUTPUT-下的結(jié)果即可
配置properties文件
將生成的加密串配置ENC(加密串)到application.properties中
# 加密所需的salt(鹽)jasypt.encryptor.password=G0CvDz7oJn6# 默認(rèn)加密方式PBEWithMD5AndDES,可以更改為PBEWithMD5AndTripleDES# jasypt.encryptor.algorithm=PBEWithMD5AndDESspring.datasource.username=ENC(6eaMh/RX5oXUVca9ignvtg==)spring.datasource.password=ENC(6eaMh/RX5oXUVca9ignvtg==)
加密方式對應(yīng)的類為BasicTextEncryptor和StrongTextEncryptor
public BasicTextEncryptor() { super(); this.encryptor = new StandardPBEStringEncryptor(); this.encryptor.setAlgorithm("PBEWithMD5AndDES");}public StrongTextEncryptor() { super(); this.encryptor = new StandardPBEStringEncryptor(); this.encryptor.setAlgorithm("PBEWithMD5AndTripleDES");}
類圖
部署時配置salt(鹽)值
為了防止salt(鹽)泄露,反解出密碼.可以在項目部署的時候使用命令傳入salt(鹽)值
java -jar -Djasypt.encryptor.password=G0CvDz7oJn6 xxx.jar
或者在服務(wù)器的環(huán)境變量里配置,進一步提高安全性
打開/etc/profile文件
vim /etc/profile
文件末尾插入
export JASYPT_PASSWORD = G0CvDz7oJn6
編譯
source /etc/profile
運行
java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
“Jasypt怎么對SpringBoot配置文件加密”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
當(dāng)前名稱:Jasypt怎么對SpringBoot配置文件加密-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://www.rwnh.cn/article20/dcehco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、做網(wǎng)站、外貿(mào)建站、網(wǎng)站營銷、電子商務(wù)、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容