wwh недель назад: 3
Родитель
Сommit
ea998d8146

+ 4 - 0
huimv-employment/fe-api/pom.xml

@@ -34,6 +34,10 @@
34 34
             <groupId>com.github.xiaoymin</groupId>
35 35
             <artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
36 36
         </dependency>
37
+        <dependency>
38
+            <groupId>com.alibaba</groupId>
39
+            <artifactId>druid-spring-boot-starter</artifactId>
40
+        </dependency>
37 41
         <dependency>
38 42
             <groupId>org.springframework.boot</groupId>
39 43
             <artifactId>spring-boot-starter-test</artifactId>

+ 61 - 0
huimv-employment/fe-api/src/main/java/com/huimv/employment/config/DecryptRedisPasswordEnvironmentPostProcessor.java

@@ -0,0 +1,61 @@
1
+package com.huimv.employment.config;
2
+
3
+import com.alibaba.druid.filter.config.ConfigTools;
4
+import org.springframework.boot.SpringApplication;
5
+import org.springframework.boot.env.EnvironmentPostProcessor;
6
+import org.springframework.core.Ordered;
7
+import org.springframework.core.env.ConfigurableEnvironment;
8
+import org.springframework.core.env.MapPropertySource;
9
+import org.springframework.util.StringUtils;
10
+
11
+import java.util.HashMap;
12
+import java.util.Map;
13
+
14
+/**
15
+ * 使用 Druid {@link ConfigTools} 与 {@code spring.datasource.druid.public-key} 解密 Redis 密码。
16
+ * <p>MySQL 密码由 Druid ConfigFilter 在连接池层解密;Redis 需在此提前写入明文到 Environment。</p>
17
+ */
18
+public class DecryptRedisPasswordEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
19
+
20
+    private static final String PUBLIC_KEY = "spring.datasource.druid.public-key";
21
+    private static final String PUBLIC_KEY_ALT = "spring.datasource.druid.publickey";
22
+    private static final String REDIS_PASSWORD = "spring.redis.password";
23
+
24
+    @Override
25
+    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
26
+        String publicKey = resolvePublicKey(environment);
27
+        if (!StringUtils.hasText(publicKey)) {
28
+            return;
29
+        }
30
+
31
+        Map<String, Object> properties = new HashMap<>(4);
32
+        properties.put(PUBLIC_KEY, publicKey);
33
+        properties.put(PUBLIC_KEY_ALT, publicKey);
34
+
35
+        String encryptedPassword = environment.getProperty(REDIS_PASSWORD);
36
+        if (StringUtils.hasText(encryptedPassword)) {
37
+            try {
38
+                properties.put(REDIS_PASSWORD, ConfigTools.decrypt(publicKey, encryptedPassword));
39
+            } catch (Exception ex) {
40
+                throw new IllegalStateException("Redis 密码解密失败,请检查 public-key 与密文是否匹配", ex);
41
+            }
42
+        }
43
+
44
+        environment.getPropertySources().addFirst(
45
+                new MapPropertySource("decryptedDruidAndRedisPassword", properties));
46
+    }
47
+
48
+    private static String resolvePublicKey(ConfigurableEnvironment environment) {
49
+        String publicKey = environment.getProperty(PUBLIC_KEY);
50
+        if (StringUtils.hasText(publicKey)) {
51
+            return publicKey;
52
+        }
53
+        return environment.getProperty(PUBLIC_KEY_ALT);
54
+    }
55
+
56
+    @Override
57
+    public int getOrder() {
58
+        // 须在 ConfigDataEnvironmentPostProcessor 加载完 application-{profile}.yml 之后再解密
59
+        return Ordered.LOWEST_PRECEDENCE;
60
+    }
61
+}

+ 21 - 0
huimv-employment/fe-api/src/main/java/com/huimv/employment/config/WebMvcConfig.java

@@ -0,0 +1,21 @@
1
+package com.huimv.employment.config;
2
+
3
+import org.springframework.context.annotation.Configuration;
4
+import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
5
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6
+
7
+/**
8
+ * Web MVC 配置。
9
+ */
10
+@Configuration
11
+public class WebMvcConfig implements WebMvcConfigurer {
12
+
13
+    /**
14
+     * 将原生 Swagger UI 重定向到 Knife4j 文档页,避免两个入口并存。
15
+     */
16
+    @Override
17
+    public void addViewControllers(ViewControllerRegistry registry) {
18
+        registry.addRedirectViewController("/swagger-ui.html", "/doc.html");
19
+        registry.addRedirectViewController("/swagger-ui/index.html", "/doc.html");
20
+    }
21
+}

+ 2 - 0
huimv-employment/fe-api/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
1
+org.springframework.boot.env.EnvironmentPostProcessor=\
2
+com.huimv.employment.config.DecryptRedisPasswordEnvironmentPostProcessor

+ 12 - 2
huimv-employment/fe-api/src/main/resources/application-dev.yml

@@ -1,4 +1,9 @@
1 1
 # 接入 MySQL / Redis 时启用:spring.profiles.active=dev
2
+#
3
+# 密码为 Druid ConfigTools RSA 加密密文,启动时需传入公钥解密,例如:
4
+#   java -jar fe-api.jar --spring.datasource.druid.publickey=你的公钥
5
+# 或在 IDE 运行配置 VM/Program arguments 中添加上述参数。
6
+# 生成新密文:fe-dao 模块运行 com.huimv.employment.dao.tools.DruidConfigToolsMain
2 7
 
3 8
 spring:
4 9
   datasource:
@@ -6,12 +11,17 @@ spring:
6 11
     driver-class-name: com.mysql.cj.jdbc.Driver
7 12
     url: jdbc:mysql://122.112.224.199:3306/fe_employment_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
8 13
     username: root
9
-    password: hm123456
14
+    password: "XyoGR4b1ODP+h3hjFieKZrO1VoHhpryX+rRbM4qbQChDnn7whnEJdAPjfBPqKBgsbbhfZR136+nG7y89I+H+UQ=="
15
+    druid:
16
+      filter:
17
+        config:
18
+          enabled: true
19
+      connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.druid.public-key:}
10 20
   redis:
11 21
     host: 122.112.224.199
12 22
     port: 6379
13 23
     database: 0
14
-    password: hm123456
24
+    password: "XyoGR4b1ODP+h3hjFieKZrO1VoHhpryX+rRbM4qbQChDnn7whnEJdAPjfBPqKBgsbbhfZR136+nG7y89I+H+UQ=="
15 25
     timeout: 3000ms
16 26
     lettuce:
17 27
       pool:

+ 3 - 1
huimv-employment/fe-api/src/main/resources/application.yml

@@ -15,7 +15,9 @@ springdoc:
15 15
   api-docs:
16 16
     enabled: true
17 17
   swagger-ui:
18
-    enabled: false
18
+    # Knife4j 依赖 /v3/api-docs/swagger-config,须保持 enabled=true
19
+    enabled: true
20
+    path: /swagger-ui.html
19 21
 
20 22
 knife4j:
21 23
   enable: true

+ 22 - 0
huimv-employment/fe-dao/src/test/java/com/huimv/employment/dao/tools/DruidConfigToolsMain.java

@@ -0,0 +1,22 @@
1
+package com.huimv.employment.dao.tools;
2
+
3
+import com.alibaba.druid.filter.config.ConfigTools;
4
+
5
+/**
6
+ * 本地生成 Druid RSA 密钥对及加密密码,运行后用于 application-dev.yml 配置。
7
+ */
8
+public class DruidConfigToolsMain {
9
+
10
+    public static void main(String[] args) throws Exception {
11
+        String plainPassword = args.length > 0 ? args[0] : "hm123456";
12
+        String[] keyPair = ConfigTools.genKeyPair(512);
13
+        String privateKey = keyPair[0];
14
+        String publicKey = keyPair[1];
15
+        String encrypted = ConfigTools.encrypt(privateKey, plainPassword);
16
+
17
+        System.out.println("plain=" + plainPassword);
18
+        System.out.println("publicKey=" + publicKey);
19
+        System.out.println("encrypted=" + encrypted);
20
+        System.out.println("verify=" + ConfigTools.decrypt(publicKey, encrypted));
21
+    }
22
+}