一、介绍
- 对配置文件内容加密,3.3.2开始支持
- 使用AES加密算法
- 实现原理:
SafetyEncryptProcessor implements EnvironmentPostProcessor
,SimpleCommandLinePropertySource
,只有在命令行的配置源读取到mpw.key才会解密
二、生成密钥
String secretKey=AES.generateRandomKey();
三、加密
String ciphertext=AES.encrypt("root",secretKey);
四、配置文件内容使用密文
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: mpw:9FAXZu54/X8se+SOnzqBdQ==
五、命令行参数
- idea的Program arguments、jar启动的参数添加
- 启动命令中添加
--mpw.key=密钥
- 环境变量+启动命令
//环境变量中添加
/etc/profile
export MPW_KEY=密钥
source /etc/profile
//启动命令添加
--mpw.key=${MPW_KEY}