<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
複製代碼
將數據庫的用戶名和密碼進行加密java
public static void main(String[] args) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(鹽)
textEncryptor.setPassword("G0CvDz7oJn6");
//要加密的數據(數據庫的用戶名或密碼)
String username = textEncryptor.encrypt("root");
String password = textEncryptor.encrypt("root123");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
複製代碼
輸出信息爲:git
username:i8QgEN4uOy2E1rHzrpSTYA==
password:6eaMh/RX5oXUVca9ignvtg==
複製代碼
或者使用Maven下載好的jar包加密\Maven\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar
github
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=G0CvDz7oJn6 algorithm=PBEWithMD5AndDES input=root
複製代碼
輸出信息爲:spring
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11
----ARGUMENTS-------------------
input: root
algorithm: PBEWithMD5AndDES
password: G0CvDz7oJn6
----OUTPUT----------------------
Gvkoz+sbFWiRe3ECtizV1A==
複製代碼
拷貝-OUTPUT-下的結果便可shell
將生成的加密串配置**ENC(加密串)**到application.properties中數據庫
# 加密所需的salt(鹽)
jasypt.encryptor.password=G0CvDz7oJn6
# 默認加密方式PBEWithMD5AndDES,能夠更改成PBEWithMD5AndTripleDES
# jasypt.encryptor.algorithm=PBEWithMD5AndDES
spring.datasource.username=ENC(6eaMh/RX5oXUVca9ignvtg==)
spring.datasource.password=ENC(6eaMh/RX5oXUVca9ignvtg==)
複製代碼
加密方式對應的類爲BasicTextEncryptor和StrongTextEncryptorvim
public BasicTextEncryptor() {
super();
this.encryptor = new StandardPBEStringEncryptor();
this.encryptor.setAlgorithm("PBEWithMD5AndDES");
}
public StrongTextEncryptor() {
super();
this.encryptor = new StandardPBEStringEncryptor();
this.encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
}
複製代碼
爲了防止salt(鹽)泄露,反解出密碼.能夠在項目部署的時候使用命令傳入salt(鹽)值安全
java -jar -Djasypt.encryptor.password=G0CvDz7oJn6 xxx.jar
複製代碼
或者在服務器的環境變量裏配置,進一步提升安全性bash
打開/etc/profile文件
vim /etc/profile
文件末尾插入
export JASYPT_PASSWORD = G0CvDz7oJn6
編譯
source /etc/profile
運行
java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
複製代碼
官方地址 : github.com/ulisesbocch…服務器