一、需求
对yml文件中敏感信息进行加密
二、实现
使用jasypt
流程
1. 引包
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2. 加盐加密敏感数据
2.1 第一种方式:在yaml中写入秘钥
jasypt:
encryptor:
password: customPwd #这里是自定义的密钥
2.2 第二种方式:命令行中写入秘钥
--jasypt.encryptor.password=customPwd
2.3 使用秘钥对敏感数据进行加密
@Autowired
StringEncryptor encryptor;
public static void main(String[] args) {
String originText = "testStr";
String encryptText = encryptor.encrypt(originText);
}
encryptText即为加密后的信息
3. 在yaml中替换
使用ENC()将需要解密的字段包起来
password: ENC(encryptText) #dsGw