前言
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組態檔裡的敏感資訊加密。是不是很簡單。