yaml配置文件加密

hpy 268 2022-08-23

一、需求

对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

4. 启动时通过2.1或2.2传入秘钥,正常启动