Spring Boot 配置文件加解密原理就這麼簡單git
接上文《失蹤人口迴歸,mybatis-plus 3.3.2 發佈》[1] ,提供了一個很是實用的功能 「數據安全保護」 功能,不只支持數據源的配置加密,對於 spring boot 全局的 yml /properties 文件都可實現敏感信息加密功能,在必定的程度上控制開發人員流動致使敏感信息泄露。web
// 數據源敏感信息加密redis
spring: datasource: url: mpw:qRhvCwF4GOqjessEB3G+a5okP+uXXr96wcucn2Pev6BfaoEMZ1gVpPPhdDmjQqoM password: mpw:Hzy5iliJbwDHhjLs1L0j6w== username: mpw:Xb+EgsyuYRXw7U7sBJjBpA== 複製代碼
// 數據源敏感信息加密spring
spring: redis: password: mpw:Hzy5iliJbwDHhjLs1L0j6w== 複製代碼
咱們翻開 spring boot 官方文檔,翻到 4.2.6 章節 Spring Boot 不提供對加密屬性值的任何內置支持,可是提供修改 Spring 環境中包含的值所必需的擴展點 EnvironmentPostProcessor 容許在應用程序以前操做環境屬性值 安全
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 配置 SPImybatis
org.springframework.boot.env.EnvironmentPostProcessor=\
com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
複製代碼
mybatis-plus 默認是讀取啓動參數,能夠在此處能夠根據本身需求修改成更安全的根密鑰存儲。app
System.getProperty("mpw.key")
複製代碼
編輯器
// 此處思路,參考 druid ConfigFilter public Properties loadConfig(String filePath) { Properties properties = new Properties(); InputStream inStream = null; 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; 複製代碼} 複製代碼if (inStream == null) { LOG.error("load config file error, file : " + filePath); return null; } if (xml) { properties.loadFromXML(inStream); } else { properties.load(inStream); } return properties; 複製代碼