使用Jasypt3.0.3版本对SpringBoot配置文件加密
时间 2023-11-01
使用Jasypt3.0.3版本对SpringBoot配置文件加密
目录
- 引入依赖
- 使用密钥生成密文
- 配置yml
- 验证是否自动解密
引入依赖
<!--yml 文件加解密--><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version></dependency>
使用密钥生成密文
@Testpublic 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: 123456algorithm: PBEWithMD5AndDESstring-output-type: Stringiv-generator-classname: org.jasypt.iv.NoIvGeneratorspring:datasource:username=ENC(i8QgEN4uOy2E1rHzrpSTYA==)password=ENC(6eaMh/RX5oXUVca9ignvtg==)
验证是否自动解密
能在本地启动程序就算成功。
部署时配置密钥值,为了防止密钥泄露,反解出密码.可以在项目部署的时候使用命令传入密钥值
java -jar -Djasypt.encryptor.password=123456 xxx.jar