Browse Source

接口解密放到gateway中处理

tags/正式3.8.0
王宁 2 years ago
parent
commit
d6bd7e3949

+ 11
- 0
gateway/pom.xml View File

@@ -33,6 +33,17 @@
33 33
             <artifactId>spring-boot-starter-test</artifactId>
34 34
             <scope>test</scope>
35 35
         </dependency>
36
+
37
+        <dependency>
38
+            <groupId>org.projectlombok</groupId>
39
+            <artifactId>lombok</artifactId>
40
+            <version>1.18.24</version>
41
+        </dependency>
42
+        <dependency>
43
+            <groupId>org.springframework</groupId>
44
+            <artifactId>spring-webmvc</artifactId>
45
+        </dependency>
46
+
36 47
     </dependencies>
37 48
     <dependencyManagement>
38 49
         <dependencies>

+ 116
- 0
gateway/src/main/java/com/xhkjedu/gateway/interceptors/DecryptRequestBodyAdvice.java View File

@@ -0,0 +1,116 @@
1
+//package com.xhkjedu.gateway.interceptors;
2
+//
3
+//import com.xhkjedu.gateway.utils.AES;
4
+//import lombok.extern.slf4j.Slf4j;
5
+//import org.springframework.core.MethodParameter;
6
+//import org.springframework.http.HttpHeaders;
7
+//import org.springframework.http.HttpInputMessage;
8
+//import org.springframework.http.converter.HttpMessageConverter;
9
+//import org.springframework.util.CollectionUtils;
10
+//import org.springframework.util.StreamUtils;
11
+//import org.springframework.web.bind.annotation.ControllerAdvice;
12
+//import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
13
+//
14
+//import javax.crypto.BadPaddingException;
15
+//import javax.crypto.IllegalBlockSizeException;
16
+//import javax.crypto.NoSuchPaddingException;
17
+//import java.io.ByteArrayInputStream;
18
+//import java.io.IOException;
19
+//import java.io.InputStream;
20
+//import java.io.UnsupportedEncodingException;
21
+//import java.lang.reflect.Type;
22
+//import java.security.InvalidKeyException;
23
+//import java.security.NoSuchAlgorithmException;
24
+//import java.util.List;
25
+//
26
+///**
27
+// * @ClassName DecryptRequestBodyAdvice
28
+// * Description TODO AES解密  (在网关中不可用)
29
+// * Author WN
30
+// * Date 2021/5/12 11:34
31
+// **/
32
+//@Slf4j
33
+//@ControllerAdvice
34
+//public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
35
+//
36
+//    private static String AES_KEY = "XINGHUOLIAOYUAN7";
37
+//
38
+//    /** 此处如果返回false , 则不执行当前Advice的业务 */
39
+//    @Override
40
+//    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
41
+////       log.info("----support----");
42
+//        return true;
43
+//    }
44
+//
45
+//    /**
46
+//     * @title 读取参数前执行
47
+//     * @description 在此做些编码 / 解密 / 封装参数为对象的操作
48
+//     **/
49
+//    @Override
50
+//    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
51
+////        log.info("----beforeBodyRead----");
52
+//
53
+//        //AES解密,false不需要解密
54
+//        List<String> decrypts = httpInputMessage.getHeaders().get("st");
55
+//        String decrypt = isListNotEmpty(decrypts)?decrypts.get(0):"false";
56
+//        if (!decrypt.equals("true")) {
57
+//            return httpInputMessage;
58
+//        }
59
+//        // 读取加密的请求体
60
+//        byte[] body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
61
+//        String str = new String(body,"utf-8");
62
+//
63
+//        try {
64
+//            // 使用AES解密
65
+//            body = AES.decryptForByte(str, AES_KEY);
66
+//        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
67
+//                BadPaddingException | UnsupportedEncodingException e) {
68
+//            e.printStackTrace();
69
+//            throw new RuntimeException(e);
70
+//        } catch (Exception e){
71
+//            e.printStackTrace();
72
+//            throw new RuntimeException(e);
73
+//        }
74
+//
75
+//        // 使用解密后的数据,构造新的读取流
76
+//        InputStream rawInputStream = new ByteArrayInputStream(body);
77
+//        return new HttpInputMessage() {
78
+//            @Override
79
+//            public HttpHeaders getHeaders() {
80
+//                return httpInputMessage.getHeaders();
81
+//            }
82
+//
83
+//            @Override
84
+//            public InputStream getBody() throws IOException {
85
+//                return rawInputStream;
86
+//            }
87
+//        };
88
+//    }
89
+//
90
+//    /**
91
+//     * @title 读取参数后执行
92
+//     * @author Xingbz
93
+//     */
94
+//    @Override
95
+//    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
96
+////        log.info("afterBodyRead:----");
97
+//        return o;
98
+//    }
99
+//
100
+//    /**
101
+//     * @title 无请求时的处理
102
+//     */
103
+//    @Override
104
+//    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
105
+////        log.info("handleEmptyBody:----");
106
+//        return o;
107
+//    }
108
+//
109
+//    private static boolean isListNotEmpty(List list) {
110
+//        if (CollectionUtils.isEmpty(list)){
111
+//            return false;
112
+//        } else {
113
+//            return true;
114
+//        }
115
+//    }
116
+//}

+ 145
- 0
gateway/src/main/java/com/xhkjedu/gateway/interceptors/DecryptRequestionBodyFilter.java View File

@@ -0,0 +1,145 @@
1
+package com.xhkjedu.gateway.interceptors;
2
+
3
+import com.xhkjedu.gateway.utils.AES;
4
+import lombok.SneakyThrows;
5
+import lombok.extern.slf4j.Slf4j;
6
+import org.springframework.cloud.gateway.filter.GatewayFilterChain;
7
+import org.springframework.cloud.gateway.filter.GlobalFilter;
8
+import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
9
+import org.springframework.cloud.gateway.support.BodyInserterContext;
10
+import org.springframework.core.Ordered;
11
+import org.springframework.core.io.buffer.DataBuffer;
12
+import org.springframework.http.HttpHeaders;
13
+import org.springframework.http.MediaType;
14
+import org.springframework.http.server.reactive.ServerHttpRequest;
15
+import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
16
+import org.springframework.http.server.reactive.ServerHttpResponse;
17
+import org.springframework.stereotype.Component;
18
+import org.springframework.util.AntPathMatcher;
19
+import org.springframework.util.CollectionUtils;
20
+import org.springframework.web.reactive.function.BodyInserter;
21
+import org.springframework.web.reactive.function.BodyInserters;
22
+import org.springframework.web.reactive.function.server.HandlerStrategies;
23
+import org.springframework.web.reactive.function.server.ServerRequest;
24
+import org.springframework.web.server.ServerWebExchange;
25
+import reactor.core.publisher.Flux;
26
+import reactor.core.publisher.Mono;
27
+
28
+import java.util.List;
29
+
30
+/**
31
+ * @Description 请求参数解密
32
+ * @Author WN
33
+ * Date 2023/2/1 14:22
34
+ **/
35
+@Component
36
+@Slf4j
37
+public class DecryptRequestionBodyFilter implements GlobalFilter, Ordered {
38
+
39
+    private static String AES_KEY = "XINGHUOLIAOYUAN7";
40
+
41
+    private AntPathMatcher antPathMatcher= new AntPathMatcher();
42
+
43
+    @SneakyThrows
44
+    @Override
45
+    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
46
+
47
+        ServerHttpRequest request = exchange.getRequest();
48
+        ServerHttpResponse response = exchange.getResponse();
49
+        String path = request.getURI().getPath(); // 当前调用方法的url
50
+        HttpHeaders headers = request.getHeaders();
51
+//        log.info("HttpMethod:{},Url:{}", request.getMethod(), request.getURI().getRawPath());
52
+        List<String> decrypts = headers.get("st");
53
+        String decrypt = isListNotEmpty(decrypts)?decrypts.get(0):"false";
54
+        if (!decrypt.equals("true")) {
55
+            return chain.filter(exchange);
56
+        }else{
57
+            return readBody(exchange, chain);
58
+        }
59
+
60
+//        //  登录跳过网关验证,检查白名单(配置)最好把不拦截路径放入配置文件,此处通过正则
61
+//        if(antPathMatcher.match("/**/api/login/auth/**",path)){
62
+//            return readBody(exchange, chain);
63
+//        }
64
+//        // 处理参数
65
+//        MediaType contentType = headers.getContentType();
66
+//        long contentLength = headers.getContentLength();
67
+//        if (contentLength > 0) {
68
+//            if (MediaType.APPLICATION_JSON.equals(contentType) || MediaType.APPLICATION_JSON_UTF8.equals(contentType)) {
69
+//                return readBody(exchange, chain);
70
+//            }
71
+//        }
72
+//        return chain.filter(exchange);
73
+    }
74
+
75
+    ServerHttpRequestDecorator decorate(ServerWebExchange exchange, HttpHeaders headers, CachedBodyOutputMessage outputMessage) {
76
+        return new ServerHttpRequestDecorator(exchange.getRequest()) {
77
+            public HttpHeaders getHeaders() {
78
+                long contentLength = headers.getContentLength();
79
+                HttpHeaders httpHeaders = new HttpHeaders();
80
+                httpHeaders.putAll(super.getHeaders());
81
+                if (contentLength > 0L) {
82
+                    httpHeaders.setContentLength(contentLength);
83
+                } else {
84
+                    httpHeaders.set("Transfer-Encoding", "chunked");
85
+                }
86
+                return httpHeaders;
87
+            }
88
+            public Flux<DataBuffer> getBody() {
89
+                return outputMessage.getBody();
90
+            }
91
+        };
92
+    }
93
+
94
+
95
+    private Mono<Void> returnMononew(GatewayFilterChain chain, ServerWebExchange exchange){
96
+        return chain.filter(exchange).then(Mono.fromRunnable(()->{
97
+        }));
98
+    }
99
+    private Mono<Void> readBody(ServerWebExchange exchange, GatewayFilterChain chain) {
100
+        //重新构造request,参考ModifyRequestBodyGatewayFilterFactory
101
+        ServerRequest serverRequest = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders());
102
+        MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
103
+        //重点
104
+        Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
105
+            //因为约定了终端传参的格式,所以只考虑json的情况,如果是表单传参,请自行增加
106
+            if (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType) || MediaType.APPLICATION_JSON_UTF8.isCompatibleWith(mediaType)) {
107
+                String newBody = null;
108
+                try{
109
+                    // 解密body
110
+                    newBody = AES.decrypt(body, AES_KEY);
111
+                }catch (Exception e){
112
+                    e.getMessage();
113
+                    log.error("解密参数出错:"+e.getMessage());
114
+                }
115
+                return Mono.just(newBody);
116
+            }
117
+            return Mono.empty();
118
+        });
119
+        BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, String.class);
120
+        HttpHeaders headers = new HttpHeaders();
121
+        headers.putAll(exchange.getRequest().getHeaders());
122
+        //猜测这个就是之前报400错误的元凶,之前修改了body但是没有重新写content length
123
+        headers.remove("Content-Length");
124
+        //MyCachedBodyOutputMessage 这个类完全就是CachedBodyOutputMessage,只不过CachedBodyOutputMessage不是公共的
125
+        CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);
126
+        return bodyInserter.insert(outputMessage, new BodyInserterContext()).then(Mono.defer(() -> {
127
+            ServerHttpRequest decorator = this.decorate(exchange, headers, outputMessage);
128
+            return returnMononew(chain, exchange.mutate().request(decorator).build());
129
+        }));
130
+    }
131
+
132
+
133
+    @Override
134
+    public int getOrder() {
135
+        return 0;
136
+    }
137
+
138
+    private boolean isListNotEmpty(List list) {
139
+        if (CollectionUtils.isEmpty(list)){
140
+            return false;
141
+        } else {
142
+            return true;
143
+        }
144
+    }
145
+}

scommons/src/main/java/com/xhkjedu/utils/AES.java → gateway/src/main/java/com/xhkjedu/gateway/utils/AES.java View File

@@ -1,11 +1,6 @@
1
-package com.xhkjedu.utils;
1
+package com.xhkjedu.gateway.utils;
2 2
 
3
-import javax.crypto.BadPaddingException;
4
-import javax.crypto.Cipher;
5
-import javax.crypto.IllegalBlockSizeException;
6
-import javax.crypto.KeyGenerator;
7
-import javax.crypto.NoSuchPaddingException;
8
-import javax.crypto.SecretKey;
3
+import javax.crypto.*;
9 4
 import javax.crypto.spec.SecretKeySpec;
10 5
 import java.io.UnsupportedEncodingException;
11 6
 import java.security.InvalidAlgorithmParameterException;

+ 0
- 109
sapi/src/main/java/com/xhkjedu/sapi/interceptors/DecryptRequestBodyAdvice.java View File

@@ -1,109 +0,0 @@
1
-package com.xhkjedu.sapi.interceptors;
2
-
3
-
4
-import com.xhkjedu.utils.AES;
5
-import com.xhkjedu.utils.N_Utils;
6
-import lombok.extern.slf4j.Slf4j;
7
-import org.springframework.core.MethodParameter;
8
-import org.springframework.http.HttpHeaders;
9
-import org.springframework.http.HttpInputMessage;
10
-import org.springframework.http.converter.HttpMessageConverter;
11
-import org.springframework.util.StreamUtils;
12
-import org.springframework.web.bind.annotation.ControllerAdvice;
13
-import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
14
-
15
-import javax.crypto.BadPaddingException;
16
-import javax.crypto.IllegalBlockSizeException;
17
-import javax.crypto.NoSuchPaddingException;
18
-import java.io.ByteArrayInputStream;
19
-import java.io.IOException;
20
-import java.io.InputStream;
21
-import java.io.UnsupportedEncodingException;
22
-import java.lang.reflect.Type;
23
-import java.security.InvalidKeyException;
24
-import java.security.NoSuchAlgorithmException;
25
-import java.util.List;
26
-
27
-/**
28
- * @ClassName DecryptRequestBodyAdvice
29
- * Description TODO AES解密
30
- * Author WN
31
- * Date 2021/5/12 11:34
32
- **/
33
-@Slf4j
34
-@ControllerAdvice
35
-public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
36
-
37
-    private static String AES_KEY = "XINGHUOLIAOYUAN7";
38
-
39
-    /** 此处如果返回false , 则不执行当前Advice的业务 */
40
-    @Override
41
-    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
42
-//       log.info("----support----");
43
-        return true;
44
-    }
45
-
46
-    /**
47
-     * @title 读取参数前执行
48
-     * @description 在此做些编码 / 解密 / 封装参数为对象的操作
49
-     **/
50
-    @Override
51
-    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
52
-//        log.info("----beforeBodyRead----");
53
-
54
-        //AES解密,false不需要解密
55
-        List<String> decrypts = httpInputMessage.getHeaders().get("st");
56
-        String decrypt = N_Utils.isListNotEmpty(decrypts)?decrypts.get(0):"false";
57
-        if (!decrypt.equals("true")) {
58
-            return httpInputMessage;
59
-        }
60
-        // 读取加密的请求体
61
-        byte[] body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
62
-        String str = new String(body,"utf-8");
63
-
64
-        try {
65
-            // 使用AES解密
66
-            body = AES.decryptForByte(str, AES_KEY);
67
-        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
68
-                BadPaddingException | UnsupportedEncodingException e) {
69
-            e.printStackTrace();
70
-            throw new RuntimeException(e);
71
-        } catch (Exception e){
72
-            e.printStackTrace();
73
-            throw new RuntimeException(e);
74
-        }
75
-
76
-        // 使用解密后的数据,构造新的读取流
77
-        InputStream rawInputStream = new ByteArrayInputStream(body);
78
-        return new HttpInputMessage() {
79
-            @Override
80
-            public HttpHeaders getHeaders() {
81
-                return httpInputMessage.getHeaders();
82
-            }
83
-
84
-            @Override
85
-            public InputStream getBody() throws IOException {
86
-                return rawInputStream;
87
-            }
88
-        };
89
-    }
90
-
91
-    /**
92
-     * @title 读取参数后执行
93
-     * @author Xingbz
94
-     */
95
-    @Override
96
-    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
97
-//        log.info("afterBodyRead:----");
98
-        return o;
99
-    }
100
-
101
-    /**
102
-     * @title 无请求时的处理
103
-     */
104
-    @Override
105
-    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
106
-//        log.info("handleEmptyBody:----");
107
-        return o;
108
-    }
109
-}

+ 0
- 108
sclass/src/main/java/com/xhkjedu/sclass/interceptors/DecryptRequestBodyAdvice.java View File

@@ -1,108 +0,0 @@
1
-package com.xhkjedu.sclass.interceptors;
2
-
3
-import com.xhkjedu.utils.AES;
4
-import com.xhkjedu.utils.N_Utils;
5
-import lombok.extern.slf4j.Slf4j;
6
-import org.springframework.core.MethodParameter;
7
-import org.springframework.http.HttpHeaders;
8
-import org.springframework.http.HttpInputMessage;
9
-import org.springframework.http.converter.HttpMessageConverter;
10
-import org.springframework.util.StreamUtils;
11
-import org.springframework.web.bind.annotation.ControllerAdvice;
12
-import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
13
-
14
-import javax.crypto.BadPaddingException;
15
-import javax.crypto.IllegalBlockSizeException;
16
-import javax.crypto.NoSuchPaddingException;
17
-import java.io.ByteArrayInputStream;
18
-import java.io.IOException;
19
-import java.io.InputStream;
20
-import java.io.UnsupportedEncodingException;
21
-import java.lang.reflect.Type;
22
-import java.security.InvalidKeyException;
23
-import java.security.NoSuchAlgorithmException;
24
-import java.util.List;
25
-
26
-/**
27
- * @ClassName DecryptRequestBodyAdvice
28
- * Description TODO AES解密
29
- * Author WN
30
- * Date 2021/5/12 11:34
31
- **/
32
-@Slf4j
33
-@ControllerAdvice
34
-public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
35
-
36
-    private static String AES_KEY = "XINGHUOLIAOYUAN7";
37
-
38
-    /** 此处如果返回false , 则不执行当前Advice的业务 */
39
-    @Override
40
-    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
41
-//       log.info("----support----");
42
-        return true;
43
-    }
44
-
45
-    /**
46
-     * @title 读取参数前执行
47
-     * @description 在此做些编码 / 解密 / 封装参数为对象的操作
48
-     **/
49
-    @Override
50
-    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
51
-//        log.info("----beforeBodyRead----");
52
-
53
-        //AES解密,false不需要解密
54
-        List<String> decrypts = httpInputMessage.getHeaders().get("st");
55
-        String decrypt = N_Utils.isListNotEmpty(decrypts)?decrypts.get(0):"false";
56
-        if (!decrypt.equals("true")) {
57
-            return httpInputMessage;
58
-        }
59
-        // 读取加密的请求体
60
-        byte[] body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
61
-        String str = new String(body,"utf-8");
62
-
63
-        try {
64
-            // 使用AES解密
65
-            body = AES.decryptForByte(str, AES_KEY);
66
-        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
67
-                BadPaddingException | UnsupportedEncodingException e) {
68
-            e.printStackTrace();
69
-            throw new RuntimeException(e);
70
-        } catch (Exception e){
71
-            e.printStackTrace();
72
-            throw new RuntimeException(e);
73
-        }
74
-
75
-        // 使用解密后的数据,构造新的读取流
76
-        InputStream rawInputStream = new ByteArrayInputStream(body);
77
-        return new HttpInputMessage() {
78
-            @Override
79
-            public HttpHeaders getHeaders() {
80
-                return httpInputMessage.getHeaders();
81
-            }
82
-
83
-            @Override
84
-            public InputStream getBody() throws IOException {
85
-                return rawInputStream;
86
-            }
87
-        };
88
-    }
89
-
90
-    /**
91
-     * @title 读取参数后执行
92
-     * @author Xingbz
93
-     */
94
-    @Override
95
-    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
96
-//        log.info("afterBodyRead:----");
97
-        return o;
98
-    }
99
-
100
-    /**
101
-     * @title 无请求时的处理
102
-     */
103
-    @Override
104
-    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
105
-//        log.info("handleEmptyBody:----");
106
-        return o;
107
-    }
108
-}

+ 0
- 109
sexam/src/main/java/com/xhkjedu/sexam/interceptors/DecryptRequestBodyAdvice.java View File

@@ -1,109 +0,0 @@
1
-package com.xhkjedu.sexam.interceptors;
2
-
3
-
4
-import com.xhkjedu.utils.AES;
5
-import com.xhkjedu.utils.N_Utils;
6
-import lombok.extern.slf4j.Slf4j;
7
-import org.springframework.core.MethodParameter;
8
-import org.springframework.http.HttpHeaders;
9
-import org.springframework.http.HttpInputMessage;
10
-import org.springframework.http.converter.HttpMessageConverter;
11
-import org.springframework.util.StreamUtils;
12
-import org.springframework.web.bind.annotation.ControllerAdvice;
13
-import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
14
-
15
-import javax.crypto.BadPaddingException;
16
-import javax.crypto.IllegalBlockSizeException;
17
-import javax.crypto.NoSuchPaddingException;
18
-import java.io.ByteArrayInputStream;
19
-import java.io.IOException;
20
-import java.io.InputStream;
21
-import java.io.UnsupportedEncodingException;
22
-import java.lang.reflect.Type;
23
-import java.security.InvalidKeyException;
24
-import java.security.NoSuchAlgorithmException;
25
-import java.util.List;
26
-
27
-/**
28
- * @ClassName DecryptRequestBodyAdvice
29
- * Description TODO AES解密
30
- * Author WN
31
- * Date 2021/5/12 11:34
32
- **/
33
-@Slf4j
34
-@ControllerAdvice
35
-public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
36
-
37
-    private static String AES_KEY = "XINGHUOLIAOYUAN7";
38
-
39
-    /** 此处如果返回false , 则不执行当前Advice的业务 */
40
-    @Override
41
-    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
42
-//       log.info("----support----");
43
-        return true;
44
-    }
45
-
46
-    /**
47
-     * @title 读取参数前执行
48
-     * @description 在此做些编码 / 解密 / 封装参数为对象的操作
49
-     **/
50
-    @Override
51
-    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
52
-//        log.info("----beforeBodyRead----");
53
-
54
-        //AES解密,false不需要解密
55
-        List<String> decrypts = httpInputMessage.getHeaders().get("st");
56
-        String decrypt = N_Utils.isListNotEmpty(decrypts)?decrypts.get(0):"false";
57
-        if (!decrypt.equals("true")) {
58
-            return httpInputMessage;
59
-        }
60
-        // 读取加密的请求体
61
-        byte[] body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
62
-        String str = new String(body,"utf-8");
63
-
64
-        try {
65
-            // 使用AES解密
66
-            body = AES.decryptForByte(str, AES_KEY);
67
-        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
68
-                BadPaddingException | UnsupportedEncodingException e) {
69
-            e.printStackTrace();
70
-            throw new RuntimeException(e);
71
-        } catch (Exception e){
72
-            e.printStackTrace();
73
-            throw new RuntimeException(e);
74
-        }
75
-
76
-        // 使用解密后的数据,构造新的读取流
77
-        InputStream rawInputStream = new ByteArrayInputStream(body);
78
-        return new HttpInputMessage() {
79
-            @Override
80
-            public HttpHeaders getHeaders() {
81
-                return httpInputMessage.getHeaders();
82
-            }
83
-
84
-            @Override
85
-            public InputStream getBody() throws IOException {
86
-                return rawInputStream;
87
-            }
88
-        };
89
-    }
90
-
91
-    /**
92
-     * @title 读取参数后执行
93
-     * @author Xingbz
94
-     */
95
-    @Override
96
-    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
97
-//        log.info("afterBodyRead:----");
98
-        return o;
99
-    }
100
-
101
-    /**
102
-     * @title 无请求时的处理
103
-     */
104
-    @Override
105
-    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
106
-//        log.info("handleEmptyBody:----");
107
-        return o;
108
-    }
109
-}

+ 0
- 108
slive/src/main/java/com/xhkjedu/slive/interceptors/DecryptRequestBodyAdvice.java View File

@@ -1,108 +0,0 @@
1
-package com.xhkjedu.slive.interceptors;
2
-
3
-import com.xhkjedu.utils.AES;
4
-import com.xhkjedu.utils.N_Utils;
5
-import lombok.extern.slf4j.Slf4j;
6
-import org.springframework.core.MethodParameter;
7
-import org.springframework.http.HttpHeaders;
8
-import org.springframework.http.HttpInputMessage;
9
-import org.springframework.http.converter.HttpMessageConverter;
10
-import org.springframework.util.StreamUtils;
11
-import org.springframework.web.bind.annotation.ControllerAdvice;
12
-import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
13
-
14
-import javax.crypto.BadPaddingException;
15
-import javax.crypto.IllegalBlockSizeException;
16
-import javax.crypto.NoSuchPaddingException;
17
-import java.io.ByteArrayInputStream;
18
-import java.io.IOException;
19
-import java.io.InputStream;
20
-import java.io.UnsupportedEncodingException;
21
-import java.lang.reflect.Type;
22
-import java.security.InvalidKeyException;
23
-import java.security.NoSuchAlgorithmException;
24
-import java.util.List;
25
-
26
-/**
27
- * @ClassName DecryptRequestBodyAdvice
28
- * Description TODO AES解密
29
- * Author WN
30
- * Date 2021/5/12 11:34
31
- **/
32
-@Slf4j
33
-@ControllerAdvice
34
-public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
35
-
36
-    private static String AES_KEY = "XINGHUOLIAOYUAN7";
37
-
38
-    /** 此处如果返回false , 则不执行当前Advice的业务 */
39
-    @Override
40
-    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
41
-//       log.info("----support----");
42
-        return true;
43
-    }
44
-
45
-    /**
46
-     * @title 读取参数前执行
47
-     * @description 在此做些编码 / 解密 / 封装参数为对象的操作
48
-     **/
49
-    @Override
50
-    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
51
-//        log.info("----beforeBodyRead----");
52
-
53
-        //AES解密,false不需要解密
54
-        List<String> decrypts = httpInputMessage.getHeaders().get("st");
55
-        String decrypt = N_Utils.isListNotEmpty(decrypts)?decrypts.get(0):"false";
56
-        if (!decrypt.equals("true")) {
57
-            return httpInputMessage;
58
-        }
59
-        // 读取加密的请求体
60
-        byte[] body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
61
-        String str = new String(body,"utf-8");
62
-
63
-        try {
64
-            // 使用AES解密
65
-            body = AES.decryptForByte(str, AES_KEY);
66
-        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
67
-                BadPaddingException | UnsupportedEncodingException e) {
68
-            e.printStackTrace();
69
-            throw new RuntimeException(e);
70
-        } catch (Exception e){
71
-            e.printStackTrace();
72
-            throw new RuntimeException(e);
73
-        }
74
-
75
-        // 使用解密后的数据,构造新的读取流
76
-        InputStream rawInputStream = new ByteArrayInputStream(body);
77
-        return new HttpInputMessage() {
78
-            @Override
79
-            public HttpHeaders getHeaders() {
80
-                return httpInputMessage.getHeaders();
81
-            }
82
-
83
-            @Override
84
-            public InputStream getBody() throws IOException {
85
-                return rawInputStream;
86
-            }
87
-        };
88
-    }
89
-
90
-    /**
91
-     * @title 读取参数后执行
92
-     * @author Xingbz
93
-     */
94
-    @Override
95
-    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
96
-//        log.info("afterBodyRead:----");
97
-        return o;
98
-    }
99
-
100
-    /**
101
-     * @title 无请求时的处理
102
-     */
103
-    @Override
104
-    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
105
-//        log.info("handleEmptyBody:----");
106
-        return o;
107
-    }
108
-}

+ 0
- 109
sstudy/src/main/java/com/xhkjedu/sstudy/interceptors/DecryptRequestBodyAdvice.java View File

@@ -1,109 +0,0 @@
1
-package com.xhkjedu.sstudy.interceptors;
2
-
3
-
4
-import com.xhkjedu.utils.AES;
5
-import com.xhkjedu.utils.N_Utils;
6
-import lombok.extern.slf4j.Slf4j;
7
-import org.springframework.core.MethodParameter;
8
-import org.springframework.http.HttpHeaders;
9
-import org.springframework.http.HttpInputMessage;
10
-import org.springframework.http.converter.HttpMessageConverter;
11
-import org.springframework.util.StreamUtils;
12
-import org.springframework.web.bind.annotation.ControllerAdvice;
13
-import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
14
-
15
-import javax.crypto.BadPaddingException;
16
-import javax.crypto.IllegalBlockSizeException;
17
-import javax.crypto.NoSuchPaddingException;
18
-import java.io.ByteArrayInputStream;
19
-import java.io.IOException;
20
-import java.io.InputStream;
21
-import java.io.UnsupportedEncodingException;
22
-import java.lang.reflect.Type;
23
-import java.security.InvalidKeyException;
24
-import java.security.NoSuchAlgorithmException;
25
-import java.util.List;
26
-
27
-/**
28
- * @ClassName DecryptRequestBodyAdvice
29
- * Description TODO AES解密
30
- * Author WN
31
- * Date 2021/5/12 11:34
32
- **/
33
-@Slf4j
34
-@ControllerAdvice
35
-public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
36
-
37
-    private static String AES_KEY = "XINGHUOLIAOYUAN7";
38
-
39
-    /** 此处如果返回false , 则不执行当前Advice的业务 */
40
-    @Override
41
-    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
42
-//       log.info("----support----");
43
-        return true;
44
-    }
45
-
46
-    /**
47
-     * @title 读取参数前执行
48
-     * @description 在此做些编码 / 解密 / 封装参数为对象的操作
49
-     **/
50
-    @Override
51
-    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
52
-//        log.info("----beforeBodyRead----");
53
-
54
-        //AES解密,false不需要解密
55
-        List<String> decrypts = httpInputMessage.getHeaders().get("st");
56
-        String decrypt = N_Utils.isListNotEmpty(decrypts)?decrypts.get(0):"false";
57
-        if (!decrypt.equals("true")) {
58
-            return httpInputMessage;
59
-        }
60
-        // 读取加密的请求体
61
-        byte[] body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
62
-        String str = new String(body,"utf-8");
63
-
64
-        try {
65
-            // 使用AES解密
66
-            body = AES.decryptForByte(str, AES_KEY);
67
-        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
68
-                BadPaddingException | UnsupportedEncodingException e) {
69
-            e.printStackTrace();
70
-            throw new RuntimeException(e);
71
-        } catch (Exception e){
72
-            e.printStackTrace();
73
-            throw new RuntimeException(e);
74
-        }
75
-
76
-        // 使用解密后的数据,构造新的读取流
77
-        InputStream rawInputStream = new ByteArrayInputStream(body);
78
-        return new HttpInputMessage() {
79
-            @Override
80
-            public HttpHeaders getHeaders() {
81
-                return httpInputMessage.getHeaders();
82
-            }
83
-
84
-            @Override
85
-            public InputStream getBody() throws IOException {
86
-                return rawInputStream;
87
-            }
88
-        };
89
-    }
90
-
91
-    /**
92
-     * @title 读取参数后执行
93
-     * @author Xingbz
94
-     */
95
-    @Override
96
-    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
97
-//        log.info("afterBodyRead:----");
98
-        return o;
99
-    }
100
-
101
-    /**
102
-     * @title 无请求时的处理
103
-     */
104
-    @Override
105
-    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
106
-//        log.info("handleEmptyBody:----");
107
-        return o;
108
-    }
109
-}

+ 0
- 108
suser/src/main/java/com/xhkjedu/suser/interceptors/DecryptRequestBodyAdvice.java View File

@@ -1,108 +0,0 @@
1
-package com.xhkjedu.suser.interceptors;
2
-
3
-import com.xhkjedu.utils.AES;
4
-import com.xhkjedu.utils.N_Utils;
5
-import lombok.extern.slf4j.Slf4j;
6
-import org.springframework.core.MethodParameter;
7
-import org.springframework.http.HttpHeaders;
8
-import org.springframework.http.HttpInputMessage;
9
-import org.springframework.http.converter.HttpMessageConverter;
10
-import org.springframework.util.StreamUtils;
11
-import org.springframework.web.bind.annotation.ControllerAdvice;
12
-import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
13
-
14
-import javax.crypto.BadPaddingException;
15
-import javax.crypto.IllegalBlockSizeException;
16
-import javax.crypto.NoSuchPaddingException;
17
-import java.io.ByteArrayInputStream;
18
-import java.io.IOException;
19
-import java.io.InputStream;
20
-import java.io.UnsupportedEncodingException;
21
-import java.lang.reflect.Type;
22
-import java.security.InvalidKeyException;
23
-import java.security.NoSuchAlgorithmException;
24
-import java.util.List;
25
-
26
-/**
27
- * @ClassName DecryptRequestBodyAdvice
28
- * Description TODO AES解密
29
- * Author WN
30
- * Date 2021/5/12 11:34
31
- **/
32
-@Slf4j
33
-@ControllerAdvice
34
-public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
35
-
36
-    private static String AES_KEY = "XINGHUOLIAOYUAN7";
37
-
38
-    /** 此处如果返回false , 则不执行当前Advice的业务 */
39
-    @Override
40
-    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
41
-//       log.info("----support----");
42
-        return true;
43
-    }
44
-
45
-    /**
46
-     * @title 读取参数前执行
47
-     * @description 在此做些编码 / 解密 / 封装参数为对象的操作
48
-     **/
49
-    @Override
50
-    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
51
-//        log.info("----beforeBodyRead----");
52
-
53
-        //AES解密,false不需要解密
54
-        List<String> decrypts = httpInputMessage.getHeaders().get("st");
55
-        String decrypt = N_Utils.isListNotEmpty(decrypts)?decrypts.get(0):"false";
56
-        if (!decrypt.equals("true")) {
57
-            return httpInputMessage;
58
-        }
59
-        // 读取加密的请求体
60
-        byte[] body = StreamUtils.copyToByteArray(httpInputMessage.getBody());
61
-        String str = new String(body,"utf-8");
62
-
63
-        try {
64
-            // 使用AES解密
65
-            body = AES.decryptForByte(str, AES_KEY);
66
-        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
67
-                BadPaddingException | UnsupportedEncodingException e) {
68
-            e.printStackTrace();
69
-            throw new RuntimeException(e);
70
-        } catch (Exception e){
71
-            e.printStackTrace();
72
-            throw new RuntimeException(e);
73
-        }
74
-
75
-        // 使用解密后的数据,构造新的读取流
76
-        InputStream rawInputStream = new ByteArrayInputStream(body);
77
-        return new HttpInputMessage() {
78
-            @Override
79
-            public HttpHeaders getHeaders() {
80
-                return httpInputMessage.getHeaders();
81
-            }
82
-
83
-            @Override
84
-            public InputStream getBody() throws IOException {
85
-                return rawInputStream;
86
-            }
87
-        };
88
-    }
89
-
90
-    /**
91
-     * @title 读取参数后执行
92
-     * @author Xingbz
93
-     */
94
-    @Override
95
-    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
96
-//        log.info("afterBodyRead:----");
97
-        return o;
98
-    }
99
-
100
-    /**
101
-     * @title 无请求时的处理
102
-     */
103
-    @Override
104
-    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
105
-//        log.info("handleEmptyBody:----");
106
-        return o;
107
-    }
108
-}

Loading…
Cancel
Save