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

作者:冷冷 时间:2023-11-23 17:48:46 

背景

接上文《失踪人口回归,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 ConfigFilter
public 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

来源:https://segmentfault.com/a/1190000022809536

标签:SpringBoot,文件,加解密
0
投稿

猜你喜欢

  • C#利用Label标签控件模拟窗体标题的移动及窗体颜色不断变换效果

    2021-06-22 16:55:51
  • Android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡

    2023-08-30 21:30:38
  • 解决Android studio模拟器启动失败的问题

    2022-06-12 00:55:01
  • C#实现生成所有不重复的组合功能示例

    2023-04-18 15:49:43
  • spring boot 如何请求后缀匹配

    2022-09-24 15:15:41
  • Android开发手册TextView属性实现效果盘点

    2022-03-10 09:29:53
  • Flutter 图片开发核心技能快速掌握教程

    2023-08-16 22:01:51
  • 一个进程间通讯同步的C#框架引荐

    2023-01-18 17:55:03
  • 利用C#版OpenCV实现圆心求取实例代码

    2022-10-28 12:51:48
  • 使用注解@Recover优化丑陋的循环详解

    2021-08-05 15:11:13
  • 一文搞懂Android RecyclerView点击展开、折叠效果的实现代码

    2023-02-17 14:52:24
  • C#并行编程之Task任务

    2023-05-30 16:54:41
  • C#7.0中新特性汇总

    2023-06-24 18:29:13
  • Java结构型设计模式中建造者模式示例详解

    2023-04-24 13:37:31
  • Android table布局开发实现简单计算器

    2021-07-15 13:05:28
  • 深入分析NTFS中文件被锁定导致Process.Start失败的详解

    2023-08-24 06:11:25
  • Android入门之AlertDialog用法实例分析

    2023-12-16 02:27:02
  • Java编程在ICPC快速IO实现源码

    2021-10-18 01:41:54
  • 基于Android平台实现拼图小游戏

    2021-07-27 16:48:12
  • Android 如何本地加载pdf文件

    2023-05-24 02:11:48
  • asp之家 软件编程 m.aspxhome.com