SpringBoot實(shí)現(xiàn)配置文件加密的方案分享
1.為什么要對(duì)配置文件關(guān)鍵信息進(jìn)行加密
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false
username: root
password: root
redis:
host: 127.0.0.1
port: 6379
password: 123456
項(xiàng)目的數(shù)據(jù)庫密碼、redis 密碼等明文展示在配置文件中會(huì)有潛在的風(fēng)險(xiǎn),比如源碼泄露,或者配置文件暴露,會(huì)導(dǎo)致相關(guān)密碼泄露,造成安全風(fēng)險(xiǎn),因此采用合適的安全防護(hù)措施是有必要的。
2.配置文件加密方案
方案1:jasypt
jasypt 是一個(gè)開源的工具類,可以方便的對(duì) SpringBoot 配置文件中的配置項(xiàng)進(jìn)行對(duì)稱加密
集成步驟
1.1 引入Jar包
在maven中引入如下依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
1.2 對(duì)敏感數(shù)據(jù)生成加密字符串
有以下三種加密方式,請(qǐng)根據(jù)自己需求和項(xiàng)目情況自行選擇:
方式1: 通過加密工具類生成(以下為代碼實(shí)例,可以直接使用)
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
/**
* 使用Jasypt對(duì)配置文件進(jìn)行加密
*/
public class JasyptUtil {
/**
* 加密算法 加密解密保證同一種算法
* jasypt3.0后,默認(rèn)支持的算法為 PBEWITHHMACSHA512ANDAES_256,這種算法安全性更高,但是需要 Java JDK 1.9+或添加JCE(Java Cryptography Extension 無限強(qiáng)度權(quán)限策略文件)
* 使用PBEWithMD5AndDES算法即可
*/
private static final String algorithm ="PBEWithMD5AndDES";
//密鑰,對(duì)稱加密使用同一個(gè)密鑰進(jìn)行加解密,密鑰自己定義,注意保管
private static final String key ="test";
public static void main(String[] args) {
//加密方法示例
String encryptInfo = encrypt("root");
System.out.println(encryptInfo);
//解密方法示例
String decryptInfo = decrypt(encryptInfo);
System.out.println(decryptInfo);
}
/**
* 加密
*
* @param value 明文
* @return
*/
public static String encrypt(String value) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
// 指定算法
config.setAlgorithm(algorithm);
// 指定秘鑰
config.setPassword(key);
encryptor.setConfig(config);
// 加密數(shù)據(jù)
return encryptor.encrypt(value);
}
/**
* 解密
*
* @param value 密文
* @return
*/
public static String decrypt(String value) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm(algorithm);
config.setPassword(key);
encryptor.setConfig(config);
// 解密數(shù)據(jù)
return encryptor.decrypt(value);
}
}
方式2: 執(zhí)行 jar包,通過命令行的模式獲?。ú煌扑],操作起來不夠方便)
java -cp ./jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password="test" algorithm=PBEWithMD5AndDES input=root
輸出結(jié)果:
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.161-b12
----ARGUMENTS-------------------
input: root
algorithm: PBEWithMD5AndDES
password: ADUMDFUOV7834*
----OUTPUT----------------------
dXTlEeOApsY5oCeCQEo4Gg==
注:
- password:用來加密的密鑰
- algorithm:加密算法 PBEWithMD5AndDES/PBEWITHHMACSHA512ANDAES_256。使用PBEWithMD5AndDES即可
- input:后接的屬性為需要加密的參數(shù)
- OUTPUT輸出的值即為加密后的值
方式三: 使用Maven插件(推薦,使用起來比較快捷)
Maven中引入jasypt插件
<plugin>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<path>file:src/main/resources/application.yml</path>
</configuration>
</plugin>
注:
path標(biāo)簽中的路徑請(qǐng)根據(jù)自己配置文件路徑配置
修改配置文件
server:
port: 8030
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: DEC(root)
password: DEC(root)
jasypt:
encryptor:
password: test
- 需要加密的數(shù)據(jù)使用DEC()包起來
- password為密鑰自定義
Maven加密命令
mvn jasypt:encrypt -Djasypt.encryptor.password=test
執(zhí)行完該命令之后,DEC中的內(nèi)容會(huì)自動(dòng)被替換為ENC(加密內(nèi)容)
Maven解密命令
mvn jasypt:decrypt -Djasypt.encryptor.password=test
1.3 使用
配置文件中按下圖添加密鑰和需要加密的屬性之后,啟動(dòng)時(shí)會(huì)自動(dòng)對(duì)敏感數(shù)據(jù)進(jìn)行解密
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: ENC(加密之后的屬性)
password: ENC(加密之后的屬性)
jasypt:
encryptor:
password: 自定義的密鑰
1.4 安全事項(xiàng)
由于該方案采用的是對(duì)稱加密,一旦泄露密鑰,相關(guān)密碼將暴露,將密鑰配置在配置文件中,非常不安全,推薦將密鑰添加到啟動(dòng)命令中,降低暴露風(fēng)險(xiǎn)
java -jar demo.jar --Djasypt.encryptor.password=test
以上就是SpringBoot實(shí)現(xiàn)配置文件加密的方案分享的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot配置文件加密的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot使用Jasypt對(duì)配置文件和數(shù)據(jù)庫密碼加密
- springboot中非容器類如何獲取配置文件數(shù)據(jù)
- 詳解SpringBoot依賴注入和使用配置文件
- SpringBoot如何從配置文件中讀取配置參數(shù)
- SpringBoot中的配置文件加載優(yōu)先級(jí)詳解
- Springboot如何實(shí)現(xiàn)對(duì)配置文件中的明文密碼加密
- SpringBoot中的YAML配置文件和日志詳解
- SpringBoot綁定配置文件中變量的四種方式總結(jié)
- SpringBoot讀取多環(huán)境配置文件的幾種方式
- SpringBoot中獲取配置文件的注解詳解
- Spring Boot 配置文件(application.yml、application-dev.yml、application-test.yml)
相關(guān)文章
詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案
本篇文章主要介紹了詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
SpringBoot+Beetl實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)庫DDL的示例代碼
最近公司里有一個(gè)新的需求,需要導(dǎo)出數(shù)據(jù)庫元數(shù)據(jù)表中存儲(chǔ)的表的 DDL 語句,而在元數(shù)據(jù)表中數(shù)據(jù)源的類型龐大,少則十幾種多則達(dá)到幾十種,各種數(shù)據(jù)庫類型都有,本篇文章就從這個(gè)模版引擎來入手介紹如何實(shí)現(xiàn)功能,需要的朋友可以參考下2025-06-06
Java實(shí)現(xiàn)利用廣度優(yōu)先遍歷(BFS)計(jì)算最短路徑的方法
這篇文章主要介紹了Java實(shí)現(xiàn)利用廣度優(yōu)先遍歷(BFS)計(jì)算最短路徑的方法,實(shí)例分析了廣度優(yōu)先遍歷算法的原理與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
Spring Boot使用yml格式進(jìn)行配置的方法
很多springboot項(xiàng)目使用的是yml格式,主要目的是方便對(duì)讀懂其他人的項(xiàng)目,下面小編通過本文給大家分享Spring Boot使用yml格式進(jìn)行配置的方法,需要的朋友參考下吧2018-04-04
SSH框架網(wǎng)上商城項(xiàng)目第30戰(zhàn)之項(xiàng)目總結(jié)(附源碼下載地址)
這篇文章主要介紹了SSH框架網(wǎng)上商城項(xiàng)目第30戰(zhàn)之項(xiàng)目總結(jié),并附源碼下載地址,感興趣的小伙伴們可以參考一下2016-06-06

