Spring Boot 配置文件加解密原理就这么简单

背景

接上文《失踪人口回归,mybatis-plus 3.3.2 发布》[1] ,提供了一个非常实用的功能 「数据安全保护」 功能,不仅支持数据源的配置加密,对于 spring boot 全局的 yml /properties 文件均可实现敏感信息加密功能,在一定的程度上控制开发人员流动导致敏感信息泄露。

// 数据源敏感信息加密

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

// 数据源敏感信息加密

spring:redis:password: mpw:Hzy5iliJbwDHhjLs1L0j6w==

实现原理

我们翻开 spring boot 官方文档,翻到 4.2.6 章节 Spring Boot 不提供对加密属性值的任何内置支持,但是提供修改 Spring 环境中包含的值所必需的扩展点 EnvironmentPostProcessor 允许在应用程序之前操作环境属性值

mybatis-plus 的实现

public class SafetyEncryptProcessor implements EnvironmentPostProcessor {@Overridepublic 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

Spring Boot 实现配置文件加解密原理相关推荐

  1. 护网必备技能:Spring Boot 接口数据加解密 功能实现

    护网必备技能:Spring Boot 接口数据加解密 功能实现 文章目录 护网必备技能:Spring Boot 接口数据加解密 功能实现 1. 尽量少改动,不影响之前的业务逻辑: 2. 考虑到时间紧迫 ...

  2. Spring Cloud Config配置文件加解密

    Spring Cloud Config配置文件加解密 坑爹的问题 > curl http://localhost:8888/encrypt -d 123{"description&qu ...

  3. Spring Boot的配置文件加载优先级

    基于Spring Boot 2.x详细介绍了Spring Boot的配置文件的加载优先级. 文章目录 1 总体优先级 2 内部配置优先级 3 bootstrap和application的优先级 1 总 ...

  4. Spring Boot默认配置文件加载顺序(四)

    通常情况下,Spring Boot 在启动时会将 resources 目录下的 application.properties 或 apllication.yml 作为其默认配置文件,我们可以在该配置文 ...

  5. Spring Boot 接口数据加解密,so easy!

    今天这篇文章聊一聊接口安全问题,涉及到接口的加密.解密 和产品.前端同学对外需求后,梳理了相关技术方案, 主要的需求点如下: 尽量少改动,不影响之前的业务逻辑: 考虑到时间紧迫性,可采用对称性加密方式 ...

  6. Spring Boot 接口数据加解密就该这样设计~

    今天这篇文章聊一聊接口安全问题,涉及到接口的加密.解密 和产品.前端同学对外需求后,梳理了相关技术方案, 主要的需求点如下: 尽量少改动,不影响之前的业务逻辑: 考虑到时间紧迫性,可采用对称性加密方式 ...

  7. Spring 配置文件加载原理

    参考:准备Spring Boot的环境 1 核心原理 ⭐️1 在SpringBoot的环境准备阶段的后期, 发布一个ApplicationEnvironmentPreparedEvent事件 ⭐️2 ...

  8. SpringBoot 系列教程(八十五):Spring Boot使用MD5加盐验签Api接口之前后端分离架构设计

    加密算法参考: 浅谈常见的七种加密算法及实现 加密算法参考: 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用 目的: 通过对API接口请求报文签名,后端进行验签处理, ...

  9. Spring Boot —— YAML配置文件

    引言 首先,YAML并不是仅仅可以使用在Java项目中,它是一种类似于json结构的标记语言,可以为所有的编程语言服务.它强调更直观的层级表示,比较适合描述配置文件中的层级关系. Spring Boo ...

最新文章

  1. 解读目标检测新范式:Segmentations is All You Need
  2. 量子力学工具箱再添利器—科学家提出高效驱动微型引擎概念
  3. 企业 SpringBoot 教程(六)springboot整合mybatis
  4. rsyncd.conf 详细配置
  5. 如何优化 Android Studio 启动、编译和运行速度?
  6. Mysql学习(三)之数据库管理工具Navicat
  7. 深入浅出Attribute (转载)
  8. Java IDEA断点调试
  9. php 商品显示,php – WooCommerce仅显示购买的商品
  10. 10款让你心动的 HTML5 CSS3 效果
  11. paip.java 开发中web server的选择jboss resin tomcat比较..
  12. Java基础三:Java 核心技术
  13. 计算机英语性考任务答案,国开电大理工英语1单元自测7形考任务答案
  14. 打工人颤抖!蓝色光标宣布:全面用 AI 代替外包,股价一度飙涨 18%!
  15. Word文档使用Mathtype如何实现公式自动居中并右对齐编号?
  16. 腾讯云学生服务器(官网校园计划)
  17. Nodejs发送https Post请求时出现socket hang up错误的解决办法汇总
  18. c# picturebox 刷新_c# – 更新PictureBox时可能导致ArgumentException的原因是什么?
  19. 软件测试需要具备的基本职业素养
  20. cef ocx 支持_Cef3/Chromium的编译和音视频支持的修改

热门文章

  1. 用米思齐(Mixly)进行Arduino编程后上传失败问题记录与解决
  2. 2021-10-14 最近给同事帮忙写了一个Excel宏
  3. Learn OpenGL 笔记6.10 SSAO(Screen Space Ambient Occlusion屏幕空间环境光遮蔽)
  4. wifi mouse hd for linux,wifi mouse电脑版(无线鼠标控制助手)V1.7.3 官方版
  5. python如何全网爬取_Python爬取全网热点榜单数据
  6. FFplay文档解读-2-语法详解
  7. 细谈Quercus----Java之PHP框架
  8. VMware 下安装Ubuntu的吐血经历
  9. 软件缺陷导致严重后果的典型案例
  10. 关于git,这一篇git命令汇总解析就够了