国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring Boot 實現配置文件加解密原理

瀏覽:3日期:2023-05-15 09:57:24

背景

接上文《失蹤人口回歸,mybatis-plus 3.3.2 發布》[1] ,提供了一個非常實用的功能 「數據安全保護」 功能,不僅支持數據源的配置加密,對于 spring boot 全局的 yml /properties 文件均可實現敏感信息加密功能,在一定的程度上控制開發人員流動導致敏感信息泄露。

// 數據源敏感信息加密

spring: datasource: url: mpw:qRhvCwF4GOqjessEB3G+a5okP+uXXr96wcucn2Pev6BfaoEMZ1gVpPPhdDmjQqoM password: mpw:Hzy5iliJbwDHhjLs1L0j6w== username: mpw:Xb+EgsyuYRXw7U7sBJjBpA==

// 數據源敏感信息加密

spring: redis: password: mpw:Hzy5iliJbwDHhjLs1L0j6w==

實現原理

我們翻開 spring boot 官方文檔,翻到 4.2.6 章節 Spring Boot 不提供對加密屬性值的任何內置支持,但是提供修改 Spring 環境中包含的值所必需的擴展點 EnvironmentPostProcessor 允許在應用程序之前操作環境屬性值

Spring Boot 實現配置文件加解密原理

mybatis-plus 的實現

public class SafetyEncryptProcessor implements EnvironmentPostProcessor { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { //命令行中獲取密鑰 String mpwKey = null; // 返回全部形式的配置源(環境變量、命令行參數、配置文件 ...) for (PropertySource<?> ps : environment.getPropertySources()) { // 判斷是否需要含有加密密碼,沒有就直接跳過 if (ps instanceof SimpleCommandLinePropertySource) { SimpleCommandLinePropertySource source = (SimpleCommandLinePropertySource) ps; mpwKey = source.getProperty('mpw.key'); break; } } //處理加密內容(獲取到原有配置,然后解密放到新的map 里面(key是原有key)) HashMap<String, Object> map = new HashMap<>(); for (PropertySource<?> ps : environment.getPropertySources()) { if (ps instanceof OriginTrackedMapPropertySource) { OriginTrackedMapPropertySource source = (OriginTrackedMapPropertySource) ps; for (String name : source.getPropertyNames()) { Object value = source.getProperty(name); if (value instanceof String) { String str = (String) value; if (str.startsWith('mpw:')) { map.put(name, AES.decrypt(str.substring(4), mpwKey)); } } } } } // 將解密的數據放入環境變量,并處于第一優先級上 (這里一定要注意,覆蓋其他配置) if (!map.isEmpty()) { environment.getPropertySources().addFirst(new MapPropertySource('custom-encrypt', map)); } }}

如何加載生效

resources/META-INF/spring.factories 配置 SPI

org.springframework.boot.env.EnvironmentPostProcessor= com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor

擴展

mybatis-plus 默認是讀取啟動參數,可以在此處可以根據自己需求修改為更安全的根密鑰存儲。

讀取環境變量

System.getProperty('mpw.key')

遠程加載密碼服務

// 此處思路,參考 druid ConfigFilterpublic Properties loadConfig(String filePath) { Properties properties = new Properties(); InputStream inStream = null; try { boolean xml = false; if (filePath.startsWith('file://')) { filePath = filePath.substring('file://'.length()); inStream = getFileAsStream(filePath); xml = filePath.endsWith('.xml'); } else if (filePath.startsWith('http://') || filePath.startsWith('https://')) { URL url = new URL(filePath); inStream = url.openStream(); xml = url.getPath().endsWith('.xml'); } else if (filePath.startsWith('classpath:')) { String resourcePath = filePath.substring('classpath:'.length()); inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath); // 在classpath下應該也可以配置xml文件吧? xml = resourcePath.endsWith('.xml'); } else { inStream = getFileAsStream(filePath); xml = filePath.endsWith('.xml'); } if (inStream == null) { LOG.error('load config file error, file : ' + filePath); return null; } if (xml) { properties.loadFromXML(inStream); } else { properties.load(inStream); } return properties; } catch (Exception ex) { LOG.error('load config file error, file : ' + filePath, ex); return null; } finally { JdbcUtils.close(inStream); } }

總結

配置文件加解密,是通過自定義擴展 EnvironmentPostProcessor 實現若項目中沒有使用最新版本 mybatis-plus ,可以參考如上自己實現,不過我推薦 jasypt-spring-boot-starter[2] ,原理類似實現了一個 EnableEncryptablePropertySourcesPostProcessor ,但是支持的加密方式更多更成熟關于 jasypt 使用可以參考源碼: https://gitee.com/log4j/pig

到此這篇關于Spring Boot 實現配置文件加解密原理的文章就介紹到這了,更多相關SpringBoot文件加解密內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 亚洲精品欧洲久久婷婷99 | 一级a毛片免费观看 | 日本三级11k影院在线 | 狠狠色丁香婷婷综合久久片 | 日本免费一区二区三区三州 | 人成精品视频三区二区一区 | 国产欧美在线不卡 | 在线中文字幕精品第5页 | 国产成人精品一区二三区2022 | 久久福利网站 | 一区二区三区视频观看 | 亚洲系列 | 国产精品理论片 | 特级a毛片 | 国产成人91 | 成人毛片视频免费网站观看 | 国产区一区二 | 国产成人亚洲合集青青草原精品 | 五月色婷婷综合开心网4438 | 99精品久久久久久久 | 国产精品免费大片一区二区 | 午夜手机福利 | 全午夜免费一级毛片 | 国产成人香蕉久久久久 | 日韩亚洲一区中文字幕 | 99热在线获取最新地址 | 午夜精品在线 | 久久亚洲精品中文字幕第一区 | 亚洲字幕| 久久福利青草免费精品 | 亚洲精美视频 | 久久香蕉国产精品一区二区三 | 精品精品国产欧美在线观看 | 日韩视频国产 | 国内视频一区二区 | 欧美日韩在线第一页 | 欧美成人se01短视频在线看 | 三级黄毛片 | 国产精品久久久久无毒 | 中文在线日韩 | 欧美日韩国产人成在线观看 |