▶ Spring Boot 依赖与配置
Maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
▶ 使用说明
假设有配置项 com.anoyi.custom.name=anoyi
不能明文显示,则可以使用 jasypt
的 PBEWithMD5AndDES
算法加密算法进行如下配置:
com.anoyi.custom.name=ENC(TqrnYZn55aFVwnSo2TrbFA==)
jasypt.encryptor.password=anoyi
-
jasypt.encryptor.password
为自定义值,用此密码加密的明文,需要用此密码解密密文 -
ENC(...)
为jasypt
提供的加密标识,Spring Boot 服务启动时,加载各种 properties 时会依据此标识判断是否解密赋值,可自定义 -
TqrnYZn55aFVwnSo2TrbFA==
为明文字符串anoyi
通过密码anoyi
加密后得到的值,此值不唯一,即同一明文通过同一密码加密会得到不同的值
▶ 配置说明
基于 Password 的加密配置
参数 | 必填 | 默认值 |
---|---|---|
jasypt.encryptor.password | True | - |
jasypt.encryptor.algorithm | False | PBEWithMD5AndDES |
jasypt.encryptor.keyObtentionIterations | False | 1000 |
jasypt.encryptor.poolSize | False | 1 |
jasypt.encryptor.providerName | False | SunJCE |
jasypt.encryptor.providerClassName | False | null |
jasypt.encryptor.saltGeneratorClassname | False | org.jasypt.salt.RandomSaltGenerator |
jasypt.encryptor.ivGeneratorClassname | False | org.jasypt.salt.NoOpIVGenerator |
jasypt.encryptor.stringOutputType | False | base64 |
jasypt.encryptor.proxyPropertySources | False | false |
最新版的 jasypt
还支持非对称加密、自定义加密器等等功能,更多信息:
▶ 配置参数加解密
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
示例加解密字符串 anoyi
@RunWith(SpringRunner.class)
@SpringBootTest
public class EncryptTest {
@Autowired
private StringEncryptor jasyptStringEncryptor;
@Test
public void encrypt() {
String encryptStr = jasyptStringEncryptor.encrypt("anoyi");
System.out.println(encryptStr);
}
@Test
public void decrypt() {
String encryptStr = jasyptStringEncryptor.decrypt("TqrnYZn55aFVwnSo2TrbFA==");
System.out.println(encryptStr);
}
}