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
package com.everyday.word.aspect;
 
 
import com.everyday.word.utils.Constant;
import com.everyday.word.utils.SystemInfoUtil;
import com.everyday.word.vo.AcceptData;
import net.sf.json.JSONObject;
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.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
 
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.*;
 
/**
 * @author hxh
 * @description 客户端签名验证
 * @date 16:33 2025/2/11
 * @return
 **/
@Component
@Aspect
@Order(2)
public class SignValidateAspect {
    public static final String EDP = "execution(* com.everyday.word.controller.client.*.*.*(..))";
 
    @Around(EDP)
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
 
        HttpServletRequest request = servletContainer.getRequest();
 
        AcceptData acceptData = null;
        for (Object obj : args) {
            if (obj instanceof AcceptData) {
                acceptData = (AcceptData) obj;
            } else if (obj instanceof HttpServletRequest) {
                request = (HttpServletRequest) obj;
            }
        }
        if (acceptData == null) {
            servletContainer.getResponse().getWriter().print(JsonUtil.loadFalseResult(-1, "签名错误"));
            return null;
        }
 
        if (acceptData != null) {
            acceptData.setSystem(SystemInfoUtil.getSystem(acceptData));
        }
        boolean isRight = signIsRight(request, acceptData.getSystem().getApiSignKey());
        // 签名是否正确
        if (isRight) {
            // 判断签名超时
            if (Math.abs((acceptData.getTimestamp() - System.currentTimeMillis())) > 1000 * 60 * 10) {
                JSONObject data = new JSONObject();
                data.put("code", -2);
                data.put("msg", "时间错误");
                PrintWriter out = servletContainer.getResponse().getWriter();
                out.print(data);
                out.close();
                return null;
            }
 
            Object obj = null;
            try {
                obj = joinPoint.proceed(args);
                // 记录大于2s的请求
            } catch (Throwable e) {
                if (!Constant.IS_TEST) {
                    PrintWriter out = servletContainer.getResponse().getWriter();
                    out.print(JsonUtil.loadFalseResult(90009, "服务器内部错误"));
                } else {
                    throw e;
                }
            }
            return obj;
        } else {
            PrintWriter out = servletContainer.getResponse().getWriter();
            out.print(JsonUtil.loadFalseResult(-1, "签名错误"));
            out.close();
            return null;
        }
    }
 
 
    /**
     * 判断签名的正确性
     *
     * @param request
     * @return
     */
    private boolean signIsRight(HttpServletRequest request, String signKey) {
        Map<String, String[]> map = request.getParameterMap();
        Iterator<String> its = map.keySet().iterator();
        List<String> list = new ArrayList<>();
        boolean fromWEB = false;
        while (its.hasNext()) {
            String key = its.next();
            if (key.equalsIgnoreCase("sign") || key.equalsIgnoreCase("callback") || key.equalsIgnoreCase("_")) {
                continue;
            }
            String[] values = map.get(key);
            list.add(key + "=" + values[0]);
        }
        Collections.sort(list);
        String str = "";
        for (String st : list) {
            str += st + "&";
        }
        String sign;
        if (!fromWEB) {
            sign = StringUtil.Md5(str + signKey);
        } else {
            sign = StringUtil.Md5(str + signKey);
        }
        if (sign.equalsIgnoreCase(request.getParameter("sign") + "")) {
            return true;
        } else {
            return false;
        }
    }
 
}