spring cloud 中使用jasypt-spring-boot-starter 配合Nacos进行配置加密
nacos
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
项目地址:https://gitcode.com/gh_mirrors/na/nacos
免费下载资源
·
引入pom
<!-- jasypt 配置文件敏感信息加解密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
数据加密
public static String encryptWithSHA512(String plainText, String factor) {
// 1. 创建加解密工具实例
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 2. 加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(factor);
config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
// 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
config.setKeyObtentionIterations( "1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
// 3. 加密
return encryptor.encrypt(plainText);
}
Nacos中配置
auth=ENC(gaAuVVD5qwYnI3/AKHGczgjxmrae9AzYE7kEsIJ7oksX4R+IwQp+6l32LmDBcIXi)
解密password配置
idea配置
-Djasypt.encryptor.password=123456
编辑application配置
配置vm参数
配置文件中配置
jar启动配置方式
Java -jar xxx.jar -Djasypt.encryptor.password=123456
验证
验证代码
@Slf4j
@Api("测试")
@RestController
@RequestMapping("test")
public class TestController {
@Value("${auth}")
private String auth;
@GetMapping("/auth")
public String auth() {
return auth;
}
}
验证返回结果
配置说明
2021-07-08 16:00:01.918 INFO 48940 --- [ main] c.u.j.c.StringEncryptorBuilder : Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWITHHMACSHA512ANDAES_256
2021-07-08 16:00:01.918 INFO 48940 --- [ main] c.u.j.c.StringEncryptorBuilder : Encryptor config not found for property jasypt.encryptor.key-obtention-iterations, using default value: 1000
2021-07-08 16:00:01.919 INFO 48940 --- [ main] c.u.j.c.StringEncryptorBuilder : Encryptor config not found for property jasypt.encryptor.pool-size, using default value: 1
2021-07-08 16:00:01.920 INFO 48940 --- [ main] c.u.j.c.StringEncryptorBuilder : Encryptor config not found for property jasypt.encryptor.provider-name, using default value: null
2021-07-08 16:00:01.920 INFO 48940 --- [ main] c.u.j.c.StringEncryptorBuilder : Encryptor config not found for property jasypt.encryptor.provider-class-name, using default value: null
2021-07-08 16:00:01.921 INFO 48940 --- [ main] c.u.j.c.StringEncryptorBuilder : Encryptor config not found for property jasypt.encryptor.salt-generator-classname, using default value: org.jasypt.salt.RandomSaltGenerator
2021-07-08 16:00:01.922 INFO 48940 --- [ main] c.u.j.c.StringEncryptorBuilder : Encryptor config not found for property jasypt.encryptor.iv-generator-classname, using default value: org.jasypt.iv.RandomIvGenerator
可在配置文件中自定义修改
- 默认加密算法 jasypt.encryptor.algorithm, using default value: PBEWITHHMACSHA512ANDAES_256
- 默认的迭代次数 jasypt.encryptor.key-obtention-iterations, using default value: 1000
- 默认池大小jasypt.encryptor.pool-size, using default value: 1
- 默认生成盐的类:org.jasypt.salt.RandomSaltGenerator
- 默认IV生成器类:org.jasypt.iv.RandomIvGenerator
- 默认IV生成器类:org.jasypt.iv.RandomIvGenerator
异常
异常信息
Exception in thread "main" org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.handleInvalidKeyException(StandardPBEByteEncryptor.java:1207)
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.encrypt(StandardPBEByteEncryptor.java:996)
at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:655)
at org.jasypt.encryption.pbe.PooledPBEStringEncryptor.encrypt(PooledPBEStringEncryptor.java:465)
at com.zuoja.mall.farm.JasypUtil.encryptWithSHA512(JasypUtil.java:78)
at com.zuoja.mall.farm.JasypUtil.main(JasypUtil.java:112)
异常原因
因为某些国家的进口管制限制,JDK默认对加解密有一定的限制,默认不能使用256长度秘钥加解密。
解决方案
解决方法就下载官方JCE无限制强度加密策略文件,将Java\jdk1.8.0_77\jre\lib\security\路径下的local_policy.jar和US_export_policy.jar覆盖即可。
官方网站提供了JCE无限制权限策略文件的下载:
操作步骤
-
下载
-
替换
找到java 安装目录jre/lib/security替换local_policy.jar和US_export_policy.jar
如果还报错就把jdk下面jre/lib/security替换local_policy.jar和US_export_policy.jar也替换
GitHub 加速计划 / na / nacos
29.83 K
12.75 K
下载
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
最近提交(Master分支:2 个月前 )
4334cd16
* Support custom client configuration timeout.(#12748)
* Add UT.(#12748) 5 天前
b04d2266
9 天前
更多推荐
已为社区贡献1条内容
所有评论(0)