Spring Boot 实现配置文件加解密原理

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 容许在应用程序以前操做环境属性值 安全

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 配置 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; 复制代码} 复制代码

总结

  • 配置文件加解密,是经过自定义扩展 EnvironmentPostProcessor 实现
  • 若项目中没有使用最新版本 mybatis-plus ,能够参考如上本身实现,不过我推荐 jasypt-spring-boot-starter[2] ,原理相似实现了一个 EnableEncryptablePropertySourcesPostProcessor ,可是支持的加密方式更多更成熟
  • 关于 jasypt 使用能够参考源码: https://gitee.com/log4j/pig[3]

image
相关文章
相关标签/搜索