前言
SpringBoot組態檔中的內容通常情況下是明文顯示,安全性就比較低一些,在application.properties或application.yml,比如mysql登陸密碼,redis登陸密碼以及第三方的密鑰等等一覽無余,這次是公安部和一些其他安全部門掃描我們代碼前我們自己做整改,這里介紹一個加解密組件,提高一些屬性配置的安全性,
jasypt由一個國外大神寫了一個springboot下的工具包
文章內容較短且通俗易懂,
druid 也可以做資料庫明文加密,jasypt任何配置都可以加密,
正文
介紹一下本次使用所有框架和中間件的版本

加入maven依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2.1.0版本是我用的時候最新版本,查看最新版本可以到
https://github.com/ulisesbocchio/jasypt-spring-boot
application.properties組態檔中增加如下內容(加解密時使用,改成任意字符都可以)
jasypt.encryptor.password: EbfYkitulv73I2p0mXI50JMXoaxZTKJ0
在測驗用例中生成加密后的秘鑰
public class Encryptor {
@Test
public void getPass() {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ0");
String url = textEncryptor.encrypt("jdbc:oracle:thin:xxxx");
String name = textEncryptor.encrypt("資料庫賬號");
String password = textEncryptor.encrypt("資料庫密碼");
//解密內容
// String url = textEncryptor.decrypt("");
// String name = textEncryptor.decrypt("");
// String password = textEncryptor.decrypt("4EyN0xDLbnP2lsaayjl8fbIctj5bVIdD");
System.out.println(url + "----------------");
System.out.println(name + "----------------");
System.out.println(password + "----------------");
Assert.assertTrue(name.length() > 0);
Assert.assertTrue(password.length() > 0);
}
}


1.可以在專案部署的時候使用命令傳入salt(鹽)值
java -jar -Djasypt.encryptor.password=G0CvDz7oJn6 xxx.jar
2.或者在服務器的環境變數里配置,進一步提高安全性
打開/etc/profile檔案
vim /etc/profile
檔案末尾插入
export JASYPT_PASSWORD = G0CvDz7oJn6
編譯
source /etc/profile
運行
java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
到此,我們就實作了springboot組態檔里的敏感資訊加密,是不是很簡單,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/215946.html
標籤:其他
上一篇:RTSP協議TSINGSEE青犀視頻平臺EasyNVR如何通過GET傳值的方式獲取通過登錄鑒權
下一篇:unlink快速入門
