1. 引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
2. 自定义加密器和注入
自定义加密器
package com.lt.javastu.jasypt;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
/**
* 自定义加密器
*/
public class MyStringEncryptor implements StringEncryptor {
/**
* 加解密算法
*/
public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";
/**
* 加解密盐值
*/
private String salt;
/**
* 加解密算法
*/
private String algorithm = PBE_ALGORITHMS_MD5_DES;
public MyStringEncryptor(String salt) {
this.salt = salt;
}
public MyStringEncryptor(String password, String algorithm) {
this.salt = password;
this.algorithm = algorithm;
}
/**
* 加密
* @param message
* @return
*/
@Override
public String encrypt(String message) {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 加解密盐值
encryptor.setConfig(getConfig(this.salt));
return encryptor.encrypt(message);
}
/**
* 解密
* @param encryptedMessage
* @return
*/
@Override
public String decrypt(String encryptedMessage) {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(getConfig(this.salt));
return encryptor.decrypt(encryptedMessage);
}
/**
* 获取配置
* @param salt 加密盐值
* @return
*/
public SimpleStringPBEConfig getConfig(String salt) {
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
// 加密盐值
config.setPassword(salt);
// 加解密算法
config.setAlgorithm(PBE_ALGORITHMS_MD5_DES);
// 设置密钥获取迭代次数
config.setKeyObtentionIterations(1000);
// 线程池大小:默认1
config.setPoolSize(1);
// 盐值生成器className
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
// 设置字符串输出类型
config.setStringOutputType("base64");
return config;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
MyStringEncryptor encryptor = new MyStringEncryptor("ltpassword");
String encrypt = encryptor.encrypt("root666*");
System.out.println(encrypt);
String decrypt = encryptor.decrypt(encrypt);
System.out.println(decrypt);
}
}
注入
package com.lt.javastu.jasypt;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JasyptConfig {
/**
* 加解密盐值
*/
@Value("${jasypt.encryptor.password}")
private String password;
@Bean("myStringEncryptor")
public StringEncryptor myStringEncryptor() {
return new MyStringEncryptor(password);
}
}
3. 配置文件示例
spring.datasource.password=ENC(z97PjQoxAU9WeA3qSMkLrk4OmiDEyPSB)
# 替换成自己的盐
jasypt.encryptor.password=ltpassword
jasypt.encryptor.bean=myStringEncryptor