Springboot整合Jasypt对配置文件中的密码加密的步骤
目录
- 1. 添加依赖
- 2. 添加jasypt配置
- 3. 编写加/解密工具类
- 4. 修改配置文件
- 5. 进一步防止密码泄露
- 5.1 自定义加密器
- 5.2 将自定义加密器添加到 Spring IOC容器中。
- 5.3修改配置文件
- 6. 加密盐值通过环境变量指定
1. 添加依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
2. 添加jasypt配置
jasypt:
encryptor:
# 盐值
password: 123
# 指定加密方式
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
property:
# 标识为加密属性的前缀
prefix: ENC(
# 标识为加密属性的后缀
suffix: )
3. 编写加/解密工具类
public class JasyptUtil {
/**
* PBE 算法
*/
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 JasyptUtil() {
}
/**
* Jasypt 加密
*
* @param encryptedStr 加密字符串
* @param password 盐值
* @return
*/
public static String encrypt(String encryptedStr, String password) {
return encrypt(encryptedStr, PBE_ALGORITHMS_MD5_DES, password);
}
/**
* Jasypt 加密
*
* @param encryptedStr 加密字符串
* @param algorithm 加密算法
* PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
* @param password 盐值
* @return
*/
public static String encrypt(String encryptedStr, String algorithm, String password) {
// StandardPBEStringEncryptor、StandardPBEBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
// 指定加密算法
config.setAlgorithm(algorithm);
// 加密盐值
config.setPassword(password);
//config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
encryptor.setConfig(config);
// 加密
return encryptor.encrypt(encryptedStr);
}
/**
* Jasypt 解密
*
* @param decryptStr 解密字符串
* @param password 盐值
* @return
*/
public static String decrypt(String decryptStr, String password) {
return decrypt(decryptStr, PBE_ALGORITHMS_MD5_DES, password);
}
/**
* Jasypt 解密
*
* @param decryptStr 解密字符串
* @param algorithm 指定解密算法:解密算法要与加密算法一一对应
* PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
* @param password 盐值
* @return
*/
public static String decrypt(String decryptStr, String algorithm, String password) {
// StandardPBEStringEncryptor、StandardPBEhttp://www.devze.comBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
// 指定解密算法:解密算法要与加密算法一一对应
config.setAlgorithm(algorithm);
// 加密秘钥
config.setPassword(password);
//config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
encryptor.setConfig(config);
// 解密
return encryptor.decrypt(decryptStr);
}
public static void main(String[] args) {
System.out.println("Supported PBE Algorithms: " + AlgorithmRegistry.getAllPBEAlgorithms());
String encryptedStr = "I am the string to be encrypted";
String algorithm = PBE_ALGORITHMS_SHA1_RC2_40;
String password = "salt";
String str = JasyptUtil.encrypt(encryptedStr, algorithm, password);
System.out.println("加密后的字符串:" + str);
System.out.println("解密后的字符串:" + JasyptUtil.decrypt(str, algorithm, password));
}
}
4. 修改配置文件
通过编写加/解密工具类得到对应的加密结果,然后将配置文件的原始明文密码替换成上一步对应的结果,并通过 ENC(加密结果) 包裹起来。
5. 进一步防止密码泄露
在上面的内容中编程客栈,使用的是默认的加密规则,这一点会让当自定义加密盐值(jasypt.encryptor.password) 泄漏时可能变得不安全。那么如何进一步防止密码泄露安全呢?
5.1 自定义加密器
为了进一步防止密码泄露,我们可以自定义加密规则。
自定义加密规则非常简单,只需要提供自定义的加密器配置类,然后通过jasypt.encryptor.bean配置指定加密配置类即可。
/**
* 自定义加密器
*/
public class MyStringEncryptor impements StringEncryptor {
/**
* 加解密PBE 算法
*/
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";
/**
* WCiEjlTJ加解密盐值
*/
private String password;
/**
* 加解密算法
*/
private String algorithm = PBE_ALGORITHMS_MD5_DES;
public MyStringEncryptor(String password) {
this.password = password;
}
public MyStringEncryptor(String password, String algorithm) {
this.password = password;
this.algorithm = algorithm;
}
@Override
public String encrypt(String message) {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 加解密盐值
encryptor.setConfig(getConfig(this.password));
return encryptor.encrypt(message);
}
@Override
public String decrypt(String encryptedMessage) {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(getConfig(this.password));
return encryptor.decrypt(encryptedMessage);
}
public SimpleStringPBEConfig getConfig(String password) {
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
// 加密盐值
config.setPassword(password);
// 加解密算法
config.setAlgorithm(PBE_ALGORITHMS_MD5_DES);
// 设置密钥获取迭代次数
config.setKeyObtentionIterations(1000);
// 线程池大小:默认1
config.setPoolSize(1);
// 盐值生成器className
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
// iv(initialization vector,初始化向量) 生成器className
config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
// 设置字符串输出类型
config.setStringOutputType("base64");
return config;
}
}
5.2 将自定义加密器添加到 Spring IOC容器中。
@Configuration
public class JasyptConfig {
/**
* 加解密盐值
*/
@Value("${jasypt.encryptor.password}")
private String password;
// @Bean("jasyptStringEncryptor")
@Bean("myStringEncryptor")
public StringEncryptor myStringEncryptor() {
return new MyStringEncryptor(password);
}
}
5.3修改配置文件
jasypt:
encryptor:
# 指定加解密在spring ioc容器中bean的名称,默认 jasyptStringEncryptor
bean: myStringEncryptor
# 盐值
passwww.devze.comword: 123
注意:Jasypt默认加解密器beanName为jasyptStringEncryptor,如果不想在配置文件中指定自定义加密器名称,需将自定义加密器beanName设置为jasyptStringEncryptor,否则将不生效。
6. 加密盐值通过环境变量指定
- 方式一:直接作为程序启动时的命令行参数
Java -jar app.jar --jasypt.encryptor.password=salt
- 方式二:直接作为程序启动时的应用环境变量
java -Djasypt.encryptor.password=salt -jar app.jar
- 方式三:直接作为系统环境变量
- 设置系统环境变量
vim /etc/profile
JASYPT_ENCRYPTOR_PASSWORD = salt
直接生效 source /etc/profile
jasypWCiEjlTJt.encryptor.password=${JASYPT_ENCRYPTOR_PASSWORD:}
到此这篇关于Springboot整合Jasypt对配置文件中的密码加密的步骤的文章就介绍到这了,更多相关Springboot Jasypt配置文件密码加密内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论