Helius
2021-06-23 0e7f27e131c3c0862d35111f8221a096f4641465
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.xzx.gc.common.request;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.common.utils.ExceptionUtils;
import com.xzx.gc.common.utils.SecurityUtil;
import com.xzx.gc.common.utils.SpringUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
 
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
 
/**
 * @desc 请求数据解密
 */
@ControllerAdvice
public class MyRequestBodyAdvice implements RequestBodyAdvice {
 
 
    @Autowired
    private BusinessUtil businessUtil;
 
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }
 
    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return body;
    }
 
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
        try {
            if ((SpringUtil.isDev()&&CollUtil.isNotEmpty(inputMessage.getHeaders().get("swagger"))&&"true".equals(inputMessage.getHeaders().get("swagger").get(0)))) {
                return inputMessage;
            }
            String objName = methodParameter.getMethod().getName();
            if(!businessUtil.isAuthMethod(objName)){
                return inputMessage;
            }
            String authKey = inputMessage.getHeaders().get("authKey").get(0);
            return new MyHttpInputMessage(inputMessage, authKey);
        } catch (Exception e) {
            ExceptionUtils.err("请求数据解密失败",e);
        }
        return inputMessage;
    }
 
    @Override
    public Object afterBodyRead(Object body, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return body;
    }
 
    class MyHttpInputMessage implements HttpInputMessage {
        private HttpHeaders headers;
 
        private InputStream body;
 
        public MyHttpInputMessage(HttpInputMessage inputMessage,String authKey) throws Exception {
            this.body =decyptBody(inputMessage.getBody(),authKey);
        }
 
        @Override
        public InputStream getBody() {
            return body;
        }
 
        @Override
        public HttpHeaders getHeaders() {
            return headers;
        }
 
        /**
         * 解密body
         * @param body
         * @return
         * @throws IOException
         */
        public InputStream decyptBody(InputStream body,String authKey) throws IOException {
            String s = IOUtils.toString(body, "UTF-8");
            String decrypt = SecurityUtil.decryptRsaAndAes(authKey,s);
            return IoUtil.toStream(decrypt, "UTF-8");
        }
 
    }
}