记录:424
场景:在Spring Boot微服务,使用jasypt-spring-boot加密和解密yml配置文件中的配置信息。
版本:JDK 1.8,Spring Boot 2.6.3,jasypt-1.9.3,jasypt-spring-boot-2.1.2, jasypt-spring-boot-3.0.5。
开源地址:/ulisesbocchio/jasypt-spring-boot
1.在Spring Boot微服务使用jasypt-spring-boot-3.0.5版本
1.1在引入依赖包
(1)依赖包
<dependency>
<groupId></groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
(2)解析
jasypt-spring-boot-3.0.5底层使用jasypt-1.9.3。在引入jasypt-spring-boot后,相关依赖包会自动引入。
1.2在中添加jasypt配置
(1)配置
在中添加jasypt配置,主要是指定加密秘钥。
jasypt:
encryptor:
password: U3buwRJdQ2023
(2)解析
jasypt-spring-boot会拿着指定的秘钥对需要解密的配置信息解密。
1.3在yml中配置已加密的信息
示例的值已经加密。
hub:
example:
password: ENC(C7KjxXpxXC/a/q1R8yCB+xkRIiHnDrDsmB8mEg3AWTvDNCf3nKiV09oZwHIS3SY9Sw1p3JfY3Ed7aWFEnVZ0rg==)
1.4在yml中配置已加密的信息存放格式
使用jasypt-spring-boot加密的信息,在yml文件中使用ENC()包裹起来。
1.5启动微服务
微服务日志:
String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
Encryptor config not found for property , using default value: PBEWITHHMACSHA512ANDAES_256
Encryptor config not found for property -obtention-iterations, using default value: 1000
Encryptor config not found for property -size, using default value: 1
Encryptor config not found for property -name, using default value: null
Encryptor config not found for property -class-name, using default value: null
Encryptor config not found for property -generator-classname, using default value:
Encryptor config not found for property -generator-classname, using default value:
Encryptor config not found for property -output-type, using default value: base64
从日志中可以看出:
:PBEWITHHMACSHA512ANDAES_256。
-generator-classname: 。
-generator-classname: 。
-output-type: base64。
1.6测试
代码:
@RestController
@RequestMapping("/hub/example/city")
@Slf4j
public class CityController {
@Value("${}")
private String cusPassword;
@GetMapping("/load01")
public Object load01() {
("测试开始...");
("从yml文件中获取=" + cusPassword);
("测试结束...");
return "执行成功";
}
}
解析:
使用注解获取的@Value("${}")属性值,是已经解密后的内容。
2.在Spring Boot微服务使用jasypt-spring-boot-2.1.2版本
2.1在引入依赖包
(1)依赖包
<dependency>
<groupId></groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
(2)解析
jasypt-spring-boot-2.1.2底层使用jasypt-1.9.3。在引入jasypt-spring-boot后,相关依赖包会自动引入。
2.2在中添加jasypt配置
(1)配置
在中添加jasypt配置,主要是指定加密秘钥。
jasypt:
encryptor:
password: U3buwRJdQ2023
(2)解析
jasypt-spring-boot会拿着指定的秘钥对需要解密的配置信息解密。
2.3在yml中配置已加密的信息
示例的值已经加密。
hub:
example:
password: ENC(/BxyrksOnj3U/HCwkRVySHRZs2s4eZveCVncPoCzHMI=)
2.4在yml中配置已加密的信息存放格式
使用jasypt-spring-boot加密的信息,在yml文件中使用ENC()包裹起来。
2.5启动微服务
微服务日志:
String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
Encryptor config not found for property , using default value: PBEWithMD5AndDES
Encryptor config not found for property -obtention-iterations, using default value: 1000
Encryptor config not found for property -size, using default value: 1
Encryptor config not found for property -name, using default value: null
Encryptor config not found for property -class-name, using default value: null
Encryptor config not found for property -generator-classname, using default value:
Encryptor config not found for property -generator-classname, using default value:
Encryptor config not found for property -output-type, using default value: base64
从日志中可以看出:
:PBEWithMD5AndDES。
-generator-classname: 。
-generator-classname: 。
-output-type: base64。
2.6测试
代码:
@RestController
@RequestMapping("/hub/example/city")
@Slf4j
public class CityController {
@Value("${}")
private String cusPassword;
@GetMapping("/load01")
public Object load01() {
("测试开始...");
("从yml文件中获取=" + cusPassword);
("测试结束...");
return "执行成功";
}
}
解析:
使用注解获取的@Value("${}")属性值,是已经解密后的内容。
3.生成加密配置
在yml文件中配置加密属性时,需先生成加密配置信息,并使用ECN()包裹起来放入yml配置中。
public class JasyptDemo {
public static void main(String[] args) {
f1_BasicTextEncryptor();
f2_AES256TextEncryptor();
}
/**
* 加密工具类:
* 加密算法: PBEWithMD5AndDES
*/
public static void f1_BasicTextEncryptor() {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
("当前加密方式: 加密类: BasicTextEncryptor, 加密算法: PBEWithMD5AndDES ");
// 1.设置秘钥
String salt = "U3buwRJdQ2023";
(salt);
// 2.加密
// 2.1加密内容
String pd = "Hangzhou20230427";
("加密前: " + pd);
// 2.2加密操作
String pdAfterEncrypt = (pd);
("加密后: " + pdAfterEncrypt);
// 3.解密操作
String pdAfterDecrypt = (pdAfterEncrypt);
("解密后: " + pdAfterDecrypt);
}
/**
* 加密工具类: .AES256TextEncryptor
* 加密算法: PBEWithHMACSHA512AndAES_256
*/
public static void f2_AES256TextEncryptor() {
AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
("当前加密方式: 加密类: AES256TextEncryptor, 加密算法: PBEWithHMACSHA512AndAES_256 ");
// 1.设置秘钥
String salt = "U3buwRJdQ2023";
(salt);
// 2.加密
// 2.1加密内容
String pd = "Hangzhou20230427";
("加密前: " + pd);
// 2.2加密操作
String pdAfterEncrypt = (pd);
("加密后: " + pdAfterEncrypt);
// 3.解密操作
String pdAfterDecrypt = (pdAfterEncrypt);
("解密后: " + pdAfterDecrypt);
}
}
-spring-boot的版本2.1.2和3.0.5比对
4.1相同点
底层都使用jasypt-1.9.3。
4.2不同点
默认使用算法不同。
jasypt-spring-boot-2.1.2默认加密算法:PBEWithMD5AndDES。
jasypt-spring-boot-3.0.5默认加密算法:PBEWithHMACSHA512AndAES_256。
因此,从jasypt-spring-boot-2.1.2切换到jasypt-spring-boot-3.0.5时,需要做适当修改。比如重新生成加密配置信息。或者指定算法。
-spring-boot的版本从2.1.2切换到3.0.5注意事项
从jasypt-spring-boot-2.1.2切换到jasypt-spring-boot-3.0.5时,在不修改版2.1.2生成的加密信息时,则需在yml中指定算法信息。
报错:
Unable to decrypt property: ... Decryption of Properties failed, make sure encryption/decryption passwords match
原因:
jasypt-spring-boot-2.1.2和jasypt-spring-boot-3.0.5默认使用加密和解密算法不一样,因此在使用jasypt-spring-boot-2.1.2时,生成的加密信息,切换到jasypt-spring-boot-3.0.5无法解密。
解决:
使用jasypt-spring-boot-3.0.5时,在yml文件中指定算法,不使用默认算法就行。
jasypt:
encryptor:
password: U3buwRJdQ2023
algorithm: PBEWithMD5AndDES
iv-generator-classname:
以上,感谢。
2023年4月27日