jasypt dubbo配置密文存放使用詳解
jasypt進(jìn)行密碼密文存放
許多項(xiàng)目里會(huì)對(duì)配置文件中的敏高文件進(jìn)行加密處理,避免了信息泄露問題。在springboot項(xiàng)目里,可以通過引入jasypt進(jìn)行密碼密文存放
jasypt使用
1.引入jasypt-spring-boot-starter包

2.配置信息
jasypt:
encryptor:
algorithm: PBEWithHMACSHA512AndAES_256
password: 123456 //salt或?qū)assword放在啟動(dòng)行參數(shù)里

3.啟動(dòng)項(xiàng)添加注解

4.生成密文
可以通過網(wǎng)上的密碼工具生成,也可以使用jasypt生成
AES256TextEncryptor stringEncryptor = new AES256TextEncryptor();
stringEncryptor.setPassword("123456"); //salt
String encrypt = stringEncryptor.encrypt("test"); //加密 test為你的密碼
String decrypt = stringEncryptor.decrypt(encrypt); //解密5.將生成的encrypt放到原來的密碼處并用ENC()包裹即可,ENC()可通過jasypt.encryptor.algorithm.property.prefix/suffix進(jìn)行配置
test: ENC(sdjflskdjfsjdflksdjalfjdslkfjksdjfkldsjkfsf) //此處放上面生成的encrypt
jasypt:
encryptor:
algorithm: PBEWithHMACSHA512AndAES_256
property:
prefix: #默認(rèn)為ENC(
suffix: #默認(rèn)為)與Dubbo聯(lián)用
它會(huì)存在一個(gè)問題,就是無法對(duì)dubbo的配置進(jìn)行加密,解決方法如下:
將下面文件添加到項(xiàng)目里,目前僅支持PBEWithHMACSHA512AndAES_256,如需要支持其他,可自行在TODO處添加處理邏輯
//JasyptPreparedEnvListener.java
package com.demo.listener;
import org.jasypt.util.text.AES256TextEncryptor;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertySource;
import org.springframework.web.context.support.StandardServletEnvironment;
import java.util.Iterator;
import java.util.Map;
/**
* use jasypt to decrypt the data
*/
@Configuration
public class JasyptPreparedEnvListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private String getSalt(StandardServletEnvironment env ){
String salt = env.getProperty("jasypt.encryptor.password");
if(salt == null || salt.isBlank()) {
return null;
}
return salt;
}
private String getConfigPrefix(StandardServletEnvironment env ){
String rst = env.getProperty("jasypt.encryptor.property.config.prefix");
if(rst == null || rst.isBlank()) {
return "CONFIG_ENC(";
}
return rst;
}
private String getConfigSuffix(StandardServletEnvironment env ){
String rst = env.getProperty("jasypt.encryptor.property.config.suffix");
if(rst == null || rst.isBlank()) {
return ")";
}
return rst;
}
private boolean isEncrypted(String value,String prefix,String suffix){
if(value == null || value.isBlank()){
return false;
}
String trimValue = value.trim();
return trimValue.startsWith(prefix) && trimValue.endsWith(suffix);
}
private String unwrapEncryptedValue(String value,String prefix,String suffix){
return value.substring(prefix.length(),value.length() - suffix.length());
}
private String getAlgorithm(StandardServletEnvironment env){
String rst = env.getProperty("jasypt.encryptor.algorithm");
if(rst == null || rst.isBlank()) {
return "PBEWithHMACSHA512AndAES_256";
}
return rst;
}
private Object getDecryptor(StandardServletEnvironment env){
String algorithm = getAlgorithm(env);
Object obj = null;
if(algorithm.equals("PBEWithHMACSHA512AndAES_256")){
String salt = getSalt(env);
if(salt != null){
obj = new AES256TextEncryptor();
((AES256TextEncryptor)obj).setPassword(salt);
}
}//TODO add other decryptor logic here
return obj;
}
private String decrypt(StandardServletEnvironment env,Object decryptpr,String value){
String algorithm = getAlgorithm(env);
if(algorithm.equals("PBEWithHMACSHA512AndAES_256")){
AES256TextEncryptor aes256TextEncryptor = (AES256TextEncryptor)decryptpr;
try{
return aes256TextEncryptor.decrypt(value);
}catch (Exception e){
System.out.println("decrypt failed: " + e);
return value;
}
} //TODO add other decryptor logic here
return value;
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
StandardServletEnvironment env =(StandardServletEnvironment) event.getEnvironment();
// 1. get decryptor
Object decryptor = getDecryptor(env);
if(decryptor == null){
return;
}
String configPrefix = getConfigPrefix(env);
String configSuffix = getConfigSuffix(env);
// 2. loop property
Iterator<PropertySource<?>> iterator = env.getPropertySources().iterator();
while (iterator.hasNext()) {
PropertySource<?> source = iterator.next();
String name = source.getName();
if (name.startsWith("applicationConfig")) {
Object o = source.getSource();
if (o instanceof Map) {
for (Map.Entry<String, Object> entry : ((Map<String, Object>) o).entrySet()) {
String key = entry.getKey();
String value = env.getProperty(key);
if (isEncrypted(value, configPrefix, configSuffix)) {
//3.解密
String newValue = decrypt(env, decryptor, unwrapEncryptedValue(value, configPrefix, configSuffix));
System.setProperty(key, newValue);
}
}
}
}
}
}
}- 在resources資源文件夾下新增META-INF/spring.factories,添加以下內(nèi)容 或在main函數(shù)啟動(dòng)里通過addListener api進(jìn)行添加
org.springframework.context.ApplicationListener = com.demo.listener.JasyptPreparedEnvListener
將需要密文存儲(chǔ)的地方使用CONFIG_ENC()包裹即可,CONFIG_ENC也可配置
jasypt:
encryptor:
algorithm: PBEWithHMACSHA512AndAES_256
property:
config:
prefix: #默認(rèn)為CONFIG_ENC(
suffix: #默認(rèn)為)它的思路就是在Springboot的ApplicationEnvironmentPreparedEvent事件里對(duì)配置文件信息進(jìn)行處理
ApplicationEnvironmentPreparedEvent: spring boot 對(duì)應(yīng)Enviroment已經(jīng)準(zhǔn)備完畢,但此時(shí)上下文context還沒有創(chuàng)建。在該監(jiān)聽中獲取到ConfigurableEnvironment后可以對(duì)配置信息做操作,例如:修改默認(rèn)的配置信息,增加額外的配置信息等等
以上就是jasypt dubbo配置密文存放使用詳解的詳細(xì)內(nèi)容,更多關(guān)于jasypt dubbo配置密文存放的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot如何將http轉(zhuǎn)https
這篇文章主要介紹了springboot如何將http轉(zhuǎn)https,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
在Spring Boot中使用Spark Streaming進(jìn)行實(shí)時(shí)數(shù)據(jù)處理和流式計(jì)算的步驟
這篇文章主要介紹了在Spring Boot中使用Spark Streaming進(jìn)行實(shí)時(shí)數(shù)據(jù)處理和流式計(jì)算,通過本文的介紹,我們了解了在Spring Boot中使用Spark Streaming進(jìn)行實(shí)時(shí)數(shù)據(jù)處理和流式計(jì)算的詳細(xì)步驟,需要的朋友可以參考下2024-03-03
Java測(cè)試題 實(shí)現(xiàn)一個(gè)注冊(cè)功能過程解析
這篇文章主要介紹了Java測(cè)試題 實(shí)現(xiàn)一個(gè)注冊(cè)功能過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Java String類簡(jiǎn)單用法實(shí)戰(zhàn)示例【字符串輸出、比較】
這篇文章主要介紹了Java String類簡(jiǎn)單用法,結(jié)合具體實(shí)例形式分析了Java使用String類實(shí)現(xiàn)字符串的輸出和比較功能相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
SpringBoot中Elasticsearch的連接配置原理與使用詳解
Elasticsearch是一種開源的分布式搜索和數(shù)據(jù)分析引擎,它可用于全文搜索、結(jié)構(gòu)化搜索、分析等應(yīng)用場(chǎng)景,本文主要介紹了SpringBoot中Elasticsearch的連接配置原理與使用詳解,感興趣的可以了解一下2023-09-09
springboot實(shí)現(xiàn)分頁(yè)功能的完整代碼
Spring Boot是一個(gè)快速開發(fā)框架,它提供了很多便捷的功能,其中包括分頁(yè)查詢,下面這篇文章主要給大家介紹了關(guān)于springboot實(shí)現(xiàn)分頁(yè)功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決
這篇文章主要介紹了springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫(kù)的認(rèn)證(springsecurity整合mybatis)
這篇文章主要介紹了一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫(kù)的認(rèn)證(springsecurity整合mybatis),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
在MyBatis的XML映射文件中<trim>元素所有場(chǎng)景下的完整使用示例代碼
在MyBatis的XML映射文件中,<trim>元素用于動(dòng)態(tài)添加SQL語(yǔ)句的一部分,處理前綴、后綴及多余的逗號(hào)或連接符,示例展示了如何在UPDATE、SELECT、INSERT和SQL片段中使用<trim>元素,以實(shí)現(xiàn)動(dòng)態(tài)的SQL構(gòu)建,感興趣的朋友一起看看吧2025-01-01

