Spring Boot集成Jasypt配置加密实战:从接入到避坑
作者:萧瑟余晖
SpringBoot集成Jasypt做配置加密,核心思路就是:把数据库密码、Redis密码这类敏感信息用 ENC(密文) 包起来写进配置文件,应用启动时Jasypt自动解密注入,业务代码完全无感。
一.集成 Springboot集成jasypt
1.第一步依赖
- jasypt的github的地址为:https://github.com/ulisesbocchio/jasypt-spring-boot
Maven 地址如下:https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter
官网地址为:
http://www.jasypt.org/cli.html
不同的版本由不同的加密算法和加密方式组成,使用时请注意按需区分
2.拿版本2.1.1做举例
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>2.1如果项目使用@SpringBootApplication或@EnableAutoConfiguration注解,在pom中加入上面的依赖即可对整个Spring的环境的配置信息进行加密解密。
2.2 ## jasypt-spring-boot 如果项目不使用@SpringBootApplication或@EnableAutoConfiguration注解,我们就使用下面的依赖,然后在配置Java类中加上注解@EnableEncryptableProperties。
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>2.1.1</version> </dependency>
@Configuration
@EnableEncryptableProperties
public class MyJasyptConfig {2.3 只对特定配置加密解密 如果不想使用以上两种方式对所有配置信息都进行加密解密的话,可以使用注解@EncryptablePropertySource指定配置文件,依赖如下:
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>2.1.1</version> </dependency>
@Configuration
@EncryptablePropertySource(name = "你要指定的配置文件名称", value = "classpath:你要指定的配置文件路径")
public class MyJasyptConfig {
}2.4使用@SpringBootApplication或@EnableAutoConfiguration注解
第一步:Application.java上增加注解@EnableEncryptableProperties(jasypt-spring-boot-starter包不需要该配置);
第二步在配置文件中加入jasypt.encryptor.password =你自己定义的加密秘钥,这是加密的秘钥;
第三步配置文件中所有明文密码替换为ENC(加密后字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==);
第四步引入一个MAVEN依赖;
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>2.1.1</version> </dependency>
3.加密方式的使用
1.第一种:进入你下载好的jar目录里运行
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=你自己定义的加密秘钥 algorithm=PBEWithMD5AndDES(加密方式,根据使用版本区分)
一般来讲jsypt 会自动被下载到 m2 仓库中,默认的地址为 C:\Users\【用户名】.m2\repository\org\jasypt\jasypt\1.9.3
![]()

结果:

- input的值就是原密码。
- password的值就是配置文件中jasypt.encryptor.password指定的值,即秘钥。注意:你命令中的password要和你在配置文件中指定的jasypt.encryptor.password要一致。
- algorithm:加密方式,根据使用版本的区分,本例子使用的2.x版本默认的加密方式
- OUTPUT:加密后的加密串
使用方式:
spring: datasource: url: jdbc:sqlserver:数据库地址 username: 数据库用户名 password: ENC(加密串)
使用格式即为ENC(你得到的加密字符串)
ENC前缀可改变,即由你自己定义格式
jasypt: encryptor: property: prefix: "ABC[" suffix: "]"
使用格式即为ABC(你得到的加密字符串)
2.第二种使用自定义配置进行加密,直接贴代码了。
@Slf4j
public class JasyptUtil {
/**
* Jasypt生成加密结果
* @param password 配置文件中设定的加密密码 jasypt.encryptor.password
* @param value 待加密值
* @return
*/
public static String encyptPwd(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(customConfigurationJasypt(password));
String result = encryptor.encrypt(value);
return result;
}
/**
* 解密
* @param password 设置盐值(加密解密密钥) jasypt.encryptor.password
* @param value 待解密密文
* @return
*/
public static String decyptPwd(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(customConfigurationJasypt(password));
encryptor.decrypt(value);
String result = encryptor.decrypt(value);
return result;
}
public static SimpleStringPBEConfig customConfigurationJasypt(String password){
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm("PBEWithMD5AndDES");// PBEWITHHMACSHA512ANDAES_256 3.x使用
config.setKeyObtentionIterations("1000");
config.setPoolSize(1);
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.salt.NoOpIVGenerator"); //org.jasypt.iv.RandomIvGenerator 3.x使用
config.setStringOutputType("base64");
return config;
}
public static void main(String[] args){
//加密 若修改了第一个参数加密password记得在配置文件同步修改
System.out.println("加密后:"+encyptPwd("ny_miyao","123456"));
//解密
System.out.println("解密后:"+decyptPwd("ny_miyao","AW0erevf9OLESBM8tvJGIQ=="));
}
}二.说说遇到的坑
1.版本问题
如果想用使用自定义的jasypt配置实现加密,那么必须注意你所使用的版本
注意区分旧和新版本
先上一段自定义的配置:
public static SimpleStringPBEConfig customConfigurationJasypt(String password){
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
config.setKeyObtentionIterations("1000");
config.setPoolSize(1);
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
return config;
}上述配置版本为
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>public static SimpleStringPBEConfig cryptor(String password){
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm("PBEWithMD5AndDES");// PBEWITHHMACSHA512ANDAES_256
config.setKeyObtentionIterations("1000");
config.setPoolSize(1);
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.salt.NoOpIVGenerator");
config.setStringOutputType("base64");
return config;
}上述配置版本:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>配置信息只有 jasypt.encryptor.password 是必须的,配置项有:
下图中的版本的配置项为旧版本使用,如果你使用的自定义配置那么需要注意你版本的默认的vGeneratorClassName的参数名字
2.x的一般 默认为org.jasypt.salt.NoOpIVGenerator,如果版本的默认名字不匹配,运行时就会报输入的参数类找不到的问题

新版本的如3.x的默认基本是和下图的一致,如果不行那具体要根据使用的具体版本去确认。

2.EncryptionOperationNotPossibleException异常
1.第一个原因2.x和3.x使用的加密算法不同,解密自然是需要其匹配相对应的算法去解密,所以如果使用你自定义配置的是话注意区分2.x的PBEWithMD5AndDES加密算法,和3.xPBEWITHHMACSHA512ANDAES_256加密算法,如果不匹配解密就会报这个异常。
2.第二个原因可能是你需要解密的加密字符串和解密需要的加密串不匹配,说穿了还是算法不一致的问题,首先要自己加密一遍,用加密得到的结果去进行解密。
3.权限问题
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 PolicyF
这个问题是jdk的无限强度管辖权政策权限不够(JCE),去下载一个JCE替换一下jre下的权限包就好。
6,7,8版本下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jce-all-download-5170447.html
方式:
将下载好的压缩包里的 local_policy.jar和US_export_policy.jar替换掉Java\jdk1.8.0_77\jre\lib\security\路径下的jar包
这里借用下别人的图

铭感信息明文存储会带有一定的安全隐患,加密秘钥的使用尽量还是放到内部代码里或者进行外部传入使用,而不是直接配置到配置文件里定义。
到此这篇关于Spring Boot集成Jasypt配置加密实战:从接入到避坑的文章就介绍到这了,更多相关SpringBoot集成jasypt配置信息加密内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
