瀏覽代碼

支持配置安全加密

hubin 5 年之前
父節點
當前提交
1bffe63ef0

+ 1 - 0
build.gradle

@@ -102,6 +102,7 @@ subprojects {
 
     tasks.withType(JavaCompile) {
         options.encoding = 'UTF-8'
+        options.warnings = false
         options.deprecation = true
         options.compilerArgs += ["-parameters"]
     }

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

@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.autoconfigure;
+
+import com.baomidou.mybatisplus.core.toolkit.AES;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.env.EnvironmentPostProcessor;
+import org.springframework.boot.env.OriginTrackedMapPropertySource;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MapPropertySource;
+import org.springframework.core.env.PropertySource;
+import org.springframework.core.env.SimpleCommandLinePropertySource;
+
+import java.util.HashMap;
+
+/**
+ * 安全加密处理器
+ *
+ * @author hubin
+ * @since 2020-05-23
+ */
+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));
+            }
+        }
+    }
+}

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

@@ -1,4 +1,6 @@
 # Auto Configure
+org.springframework.boot.env.EnvironmentPostProcessor=\
+  com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
   com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
   com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration