聊聊mybatis-plus的SafetyEncryptProcessor

本文主要研究一下mybatis-plus的SafetyEncryptProcessor

SafetyEncryptProcessor

mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/SafetyEncryptProcessor.java

复制代码
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;
            }
        }
        /**
         * 处理加密内容
         */
        if (StringUtils.isNotBlank(mpwKey)) {
            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 (CollectionUtils.isNotEmpty(map)) {
                environment.getPropertySources().addFirst(new MapPropertySource("custom-encrypt", map));
            }
        }
    }
}

SafetyEncryptProcessor实现了EnvironmentPostProcessor接口,在postProcessEnvironment方法中先是找到mpw.key,然后遍历所有PropertySource的所有属性,找到mpw:开头的,然后进行解密并替换到密文,最后放在environment的第一个PropertySource

spring.factories

mybatis-plus-boot-starter/src/main/resources/META-INF/spring.factories

复制代码
# Auto Configure
org.springframework.boot.env.EnvironmentPostProcessor=\
  com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor

小结

之前的文章聊聊springboot的EnvironmentPostProcessor提到springboot提供了EnvironmentPostProcessor接口,该接口有postProcessEnvironment方法,其中envrionment参数类型为ConfigurableEnvironment,即应用可以通过实现这个接口进行env环境变量的操作。而mybatis-plus的SafetyEncryptProcessor正是一个实战的好例子。

相关推荐
敲个大西瓜3 天前
mybatis拦截器插件实现数据库字段加解密
mybatis
武子康3 天前
Java-28 深入浅出 Spring 实现简易Ioc-04 在上节的业务下手动实现AOP
java·后端·mybatis
一条泥憨鱼4 天前
苍穹外卖【day6|微信登录与商品浏览功能】
后端·mybatis·苍穹外卖
vx-Biye_Design4 天前
springboot安阳地区研学旅游服务小程序-计算机毕业设计源码12785
java·vue.js·windows·spring boot·tomcat·maven·mybatis
摇滚侠4 天前
MyBatis+Spring+SpringMVC SSM 整合 179-185
java·spring·mybatis
摇滚侠4 天前
MyBatis+Spring+SpringMVC SSM ContextLoaderListener 177-178
java·spring·mybatis
Spring小子4 天前
【Spring Boot + Vue + DeepSeek】从零打造一个AI驱动的智能健康分析系统
java·spring boot·mybatis
武子康4 天前
Java-27 深入浅出 Spring - 实现简易Ioc-03 在上节的业务下手动实现IoC 从 XML 配置到 BeanFactory 反射注入
java·后端·mybatis
柏舟飞流4 天前
Spring Boot 进阶实战:整合 MyBatis、Redis、JWT,搭一个更像真实项目的后端服务
spring boot·redis·mybatis