jasypt dubbo配置密文存放使用詳解
jasypt進行密碼密文存放
許多項目里會對配置文件中的敏高文件進行加密處理,避免了信息泄露問題。在springboot項目里,可以通過引入jasypt進行密碼密文存放
jasypt使用
1.引入jasypt-spring-boot-starter包
2.配置信息
jasypt: encryptor: algorithm: PBEWithHMACSHA512AndAES_256 password: 123456 //salt
或?qū)assword放在啟動行參數(shù)里
3.啟動項添加注解
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進行配置
test: ENC(sdjflskdjfsjdflksdjalfjdslkfjksdjfkldsjkfsf) //此處放上面生成的encrypt
jasypt: encryptor: algorithm: PBEWithHMACSHA512AndAES_256 property: prefix: #默認為ENC( suffix: #默認為)
與Dubbo聯(lián)用
它會存在一個問題,就是無法對dubbo的配置進行加密,解決方法如下:
將下面文件添加到項目里,目前僅支持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ù)啟動里通過addListener api進行添加
org.springframework.context.ApplicationListener = com.demo.listener.JasyptPreparedEnvListener
將需要密文存儲的地方使用CONFIG_ENC()包裹即可,CONFIG_ENC也可配置
jasypt: encryptor: algorithm: PBEWithHMACSHA512AndAES_256 property: config: prefix: #默認為CONFIG_ENC( suffix: #默認為)
它的思路就是在Springboot的ApplicationEnvironmentPreparedEvent事件里對配置文件信息進行處理
ApplicationEnvironmentPreparedEvent: spring boot 對應Enviroment已經(jīng)準備完畢,但此時上下文context還沒有創(chuàng)建。在該監(jiān)聽中獲取到ConfigurableEnvironment后可以對配置信息做操作,例如:修改默認的配置信息,增加額外的配置信息等等
以上就是jasypt dubbo配置密文存放使用詳解的詳細內(nèi)容,更多關(guān)于jasypt dubbo配置密文存放的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot如何將http轉(zhuǎn)https
這篇文章主要介紹了springboot如何將http轉(zhuǎn)https,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04在Spring Boot中使用Spark Streaming進行實時數(shù)據(jù)處理和流式計算的步驟
這篇文章主要介紹了在Spring Boot中使用Spark Streaming進行實時數(shù)據(jù)處理和流式計算,通過本文的介紹,我們了解了在Spring Boot中使用Spark Streaming進行實時數(shù)據(jù)處理和流式計算的詳細步驟,需要的朋友可以參考下2024-03-03Java String類簡單用法實戰(zhàn)示例【字符串輸出、比較】
這篇文章主要介紹了Java String類簡單用法,結(jié)合具體實例形式分析了Java使用String類實現(xiàn)字符串的輸出和比較功能相關(guān)操作技巧,需要的朋友可以參考下2019-07-07SpringBoot中Elasticsearch的連接配置原理與使用詳解
Elasticsearch是一種開源的分布式搜索和數(shù)據(jù)分析引擎,它可用于全文搜索、結(jié)構(gòu)化搜索、分析等應用場景,本文主要介紹了SpringBoot中Elasticsearch的連接配置原理與使用詳解,感興趣的可以了解一下2023-09-09springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決
這篇文章主要介紹了springcloud本地調(diào)試feign調(diào)用出現(xiàn)的詭異404問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫的認證(springsecurity整合mybatis)
這篇文章主要介紹了一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫的認證(springsecurity整合mybatis),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10在MyBatis的XML映射文件中<trim>元素所有場景下的完整使用示例代碼
在MyBatis的XML映射文件中,<trim>元素用于動態(tài)添加SQL語句的一部分,處理前綴、后綴及多余的逗號或連接符,示例展示了如何在UPDATE、SELECT、INSERT和SQL片段中使用<trim>元素,以實現(xiàn)動態(tài)的SQL構(gòu)建,感興趣的朋友一起看看吧2025-01-01