admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.yeshi.fanli.aspect;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.yeshi.utils.JsonUtil;
 
import com.yeshi.fanli.dto.WXMPAcceptData;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
 
import net.sf.json.JSONObject;
 
/**
 * 微信小程序接口签名验证
 * 
 * @author Administrator
 *
 */
//@Component
@Aspect
@Order(2)
public class WXMPSignValidateAspect {
    public static final String EDP = "execution(* com.yeshi.fanli.controller.wxmp..*.*(..))";
 
    public static String KEY = "";
 
    static {
        KEY = Constant.systemCommonConfig.getSignKey();
    }
 
    @Around(EDP)
    public Object testAround(ProceedingJoinPoint joinPoint) throws IOException {
        Object[] args = joinPoint.getArgs();
        PrintWriter out = null;
        ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
 
        out = servletContainer.getResponse().getWriter();
        HttpServletRequest request = servletContainer.getRequest();
 
        WXMPAcceptData acceptData = null;
        for (Object obj : args) {
            if (obj instanceof WXMPAcceptData) {
                acceptData = (WXMPAcceptData) obj;
            } else if (obj instanceof HttpServletRequest) {
                request = (HttpServletRequest) obj;
            }
        }
        boolean isRight = true;
        if (acceptData == null) {
            out.print(JsonUtil.loadFalseResult(-1, "签名错误"));
            return null;
        }
 
        isRight = signIsRight(request);
 
        // 签名是否正确
        if (isRight) {
            // 判断签名超时
            if (Math.abs((acceptData.getTimeStamp()) - System.currentTimeMillis()) > 1000 * 60 * 10) {
                JSONObject data = new JSONObject();
                data.put("code", -2);
                data.put("msg", "时间错误");
                out.print(data);
                out.close();
                return null;
            }
 
            final String url = request.getRequestURI();
            @SuppressWarnings("unchecked")
            final Map<String, Object> params = request.getParameterMap();
            ThreadUtil.run(new Runnable() {
                @Override
                public void run() {
                    // 记录请求日志
                    LogHelper.requestInfo(url, params);
                }
            });
 
            // 设置device
            if (!StringUtil.isNullOrEmpty(acceptData.getOpenId())) {
                acceptData.setDevice(acceptData.getAppId() + "-" + acceptData.getOpenId());
            }
 
            Object obj = null;
            try {
                long startTime = System.currentTimeMillis();
                obj = joinPoint.proceed(args);
                final long responseTime = System.currentTimeMillis() - startTime;
                // 记录大于2s的请求
                if (responseTime >= 2000) {
                    ThreadUtil.run(new Runnable() {
 
                        @Override
                        public void run() {
                            LogHelper.requestTime(url, params, responseTime);
                        }
                    });
 
                }
 
            } catch (Throwable e) {
                LogHelper.errorDetailInfo(e, getHttpServletParams(request), request.getRequestURI().toString());
                out.print(JsonUtil.loadFalseResult(90009, "服务器内部错误"));
            }
            return obj;
        } else {
            JSONObject data = new JSONObject();
            data.put("code", -1);
            data.put("msg", "签名错误");
            out.print(data);
            out.close();
            LogHelper.error("签名错误:" + request.getRequestURI() + "-" + getHttpServletParams(request));
            return null;
        }
    }
 
    /**
     * 
     * 
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    private boolean signIsRight(HttpServletRequest request) {
        Map<String, Object> map = request.getParameterMap();
        Iterator<String> its = map.keySet().iterator();
        List<String> list = new ArrayList<>();
 
        while (its.hasNext()) {
            String key = its.next();
            if (key.equalsIgnoreCase("sign")) {
                continue;
            }
            Object value = map.get(key);
            Object[] values = (Object[]) value;
            list.add(key + "=" + values[0].toString());
        }
        Collections.sort(list);
        String str = "";
        for (String st : list) {
            str += st + "&";
        }
        String sign = null;
 
        sign = StringUtil.Md5(str + Constant.WXMP_SIGN_KEY);
        if (sign.equalsIgnoreCase(request.getParameter("sign") + "")) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 获取请求参数信息
     * 
     * @param request
     * @return
     */
    private String getHttpServletParams(HttpServletRequest request) {
        if (request == null) {
            return "";
        }
        Map map = request.getParameterMap();
        if (map != null) {
            Iterator<String> its = map.keySet().iterator();
            JSONObject json = new JSONObject();
            while (its.hasNext()) {
                String next = its.next();
                if (map.get(next) != null) {
                    Object[] objects = (Object[]) map.get(next);
                    if (objects != null && objects.length > 0) {
                        json.put(next, objects[0].toString());
                    }
                }
            }
            return json.toString();
        }
        return "";
    }
 
}