时间 2023-11-01
使用Jasypt3.0.3版本对SpringBoot配置文件加密
引入依赖
<!--yml 文件加解密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
使用密钥生成密文
@Test
public void testEncrypt(){
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
// 加密密钥
textEncryptor.setPassword("123456");
String username = textEncryptor.encrypt("your-username");
String password = textEncryptor.encrypt("your-password");
System.out.println("username:" + username);
System.out.println("password:" + password);
}
输出结果
username:i8QgEN4uOy2E1rHzrpSTYA==
password:6eaMh/RX5oXUVca9ignvtg==
配置yml
# yml文件敏感信息加解密
jasypt:
encryptor:
property:
prefix: "ENC("
suffix: ")"
password: 123456
algorithm: PBEWithMD5AndDES
string-output-type: String
iv-generator-classname: org.jasypt.iv.NoIvGenerator
spring:
datasource:
username=ENC(i8QgEN4uOy2E1rHzrpSTYA==)
password=ENC(6eaMh/RX5oXUVca9ignvtg==)
验证是否自动解密
能在本地启动程序就算成功。
部署时配置密钥值,为了防止密钥泄露,反解出密码.可以在项目部署的时候使用命令传入密钥值
java -jar -Djasypt.encryptor.password=123456 xxx.jar