Springboot整合Jasypt對配置文件中的密碼加密的步驟
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、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.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. 進一步防止密碼泄露
在上面的內容中,使用的是默認的加密規(guī)則,這一點會讓當自定義加密鹽值(jasypt.encryptor.password) 泄漏時可能變得不安全。那么如何進一步防止密碼泄露安全呢?
5.1 自定義加密器
為了進一步防止密碼泄露,我們可以自定義加密規(guī)則。
自定義加密規(guī)則非常簡單,只需要提供自定義的加密器配置類,然后通過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";
/**
* 加解密鹽值
*/
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);
// 設置密鑰獲取迭代次數(shù)
config.setKeyObtentionIterations(1000);
// 線程池大?。耗J1
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
# 鹽值
password: 123注意:Jasypt默認加解密器beanName為jasyptStringEncryptor,如果不想在配置文件中指定自定義加密器名稱,需將自定義加密器beanName設置為jasyptStringEncryptor,否則將不生效。
6. 加密鹽值通過環(huán)境變量指定
- 方式一:直接作為程序啟動時的命令行參數(shù)
java -jar app.jar --jasypt.encryptor.password=salt
- 方式二:直接作為程序啟動時的應用環(huán)境變量
java -Djasypt.encryptor.password=salt -jar app.jar
- 方式三:直接作為系統(tǒng)環(huán)境變量
- 設置系統(tǒng)環(huán)境變量
vim /etc/profile
JASYPT_ENCRYPTOR_PASSWORD = salt
直接生效 source /etc/profile
2. Spring Boot的項目配置文件指定系統(tǒng)環(huán)境變量
jasypt.encryptor.password=${JASYPT_ENCRYPTOR_PASSWORD:}到此這篇關于Springboot整合Jasypt對配置文件中的密碼加密的步驟的文章就介紹到這了,更多相關Springboot Jasypt配置文件密碼加密內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于mybatis-plus-generator實現(xiàn)代碼自動生成器
這篇文章專門為小白準備了入門級mybatis-plus-generator代碼自動生成器,可以提高開發(fā)效率。文中的示例代碼講解詳細,感興趣的可以了解一下2022-05-05
java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫
這篇文章主要介紹了java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
SpringBoot使用spring-boot-starter-validation實現(xiàn)參數(shù)校驗
在開發(fā) RESTful 接口時,接口參數(shù)校驗是保障系統(tǒng)健壯性和安全性的重要一環(huán),本文將帶你從零開始掌握如何在 Spring Boot 中使用 spring-boot-starter-validation,并通過多個實際案例演示其強大功能,需要的可以了解下2025-06-06
"Method?Not?Allowed"405問題分析以及解決方法
項目中在提交表單時,提示“HTTP 405”錯誤——“Method Not Allowed”這里顯示的是,方法不被允許,下面這篇文章主要給大家介紹了關于"Method?Not?Allowed"405問題分析以及解決方法的相關資料,需要的朋友可以參考下2022-10-10
Java Spring MVC獲取請求數(shù)據(jù)詳解操作
Spring MVC 是 Spring 提供的一個基于 MVC 設計模式的輕量級 Web 開發(fā)框架,本質上相當于 Servlet,Spring MVC 角色劃分清晰,分工明細。由于 Spring MVC 本身就是 Spring 框架的一部分,可以說和 Spring 框架是無縫集成2021-11-11
Mybatis的SqlRunner執(zhí)行流程實現(xiàn)
MyBatis提供了一個用于操作數(shù)據(jù)庫的SqlRunner工具類,對JDBC做了很好的封裝,本文主要介紹了Mybatis的SqlRunner執(zhí)行流程實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-10-10
hotspot解析jdk1.8?Unsafe類park和unpark方法使用
這篇文章主要為大家介紹了hotspot解析jdk1.8?Unsafe類park和unpark方法使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

