当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot 实现配置文件加解密原理

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

2020年06月23日  | 移动技术网IT编程  | 我要评论

背景

接上文《失踪人口回归,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 允许在应用程序之前操作环境属性值

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 使用可以参考源码:

到此这篇关于spring boot 实现配置文件加解密原理的文章就介绍到这了,更多相关springboot文件加解密内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网