最新版依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
原理
当读取到如下所示带ENC()包裹的内容,会调用jasypt进行解密后再使用。
password= ENC(密文)
生成密码样例类:
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.iv.RandomIvGenerator;
public class JasyptGenerator {
public static void main(String[] args) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
encryptor.setPassword("abcdefg");
encryptor.setIvGenerator(new RandomIvGenerator());
// 加密
String encryptText = encryptor.encrypt("7ujm8ik,8ikjjjkl;l");
System.out.println("7ujm8ik,8ikjjjkl;l,加密后的信息:" + encryptText);
}
}
spring-boot使用
- 运行spring-boot项目idea加上程序参数:
--jasypt.encryptor.password=abcdefg - 运行jar程序时加上
-Djasypt.encryptor.password=abcdefg