1. 添加maven依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2. 在application.yml中配置jasypt参数
# 可以配置一些重要的属性,可以直接进入配置类查看
jasypt:
encryptor:
# 秘钥,必须配
password: 1234
# 加密算法,有默认值“PBEWithMD5AndDES”,也可以自定义
# 支持算法可查看 http://www.jasypt.org/cli.html
algorithm: PBEWithMD5AndDES
3. 生成密文
@RunWith(SpringRunner.class)
@SpringBootTest
public class EncryptorTest {
@Autowired
StringEncryptor encryptor;
@Test
public void getPass() {
String name = encryptor.encrypt("abc");
String password = encryptor.encrypt("123");
System.out.println(name);
System.out.println(password);
}
}
4. 将密文设置到配置文件中
spring:
datasource:
username: ENC(密文)
password: ENC(密文)
5. 其他配置项
KEY | REQUIRED | DEFAULT VALUE |
---|---|---|
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 |
6.jasypt扩展资料
jasypt-spring-boot详细使用说明https://github.com/ulisesbocchio/jasypt-spring-boot
jasypt官网http://www.jasypt.org/cli.html