yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
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
package com.qianwen.core.secure.interceptor;
 
import java.time.Duration;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.secure.props.SignSecure;
import com.qianwen.core.secure.provider.HttpMethod;
import com.qianwen.core.secure.provider.ResponseProvider;
import com.qianwen.core.tool.jackson.JsonUtil;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.DigestUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.WebUtil;
import org.springframework.lang.NonNull;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
/* loaded from: blade-core-secure-9.3.0.0-SNAPSHOT.jar:org/springblade/core/secure/interceptor/SignInterceptor.class */
public class SignInterceptor extends HandlerInterceptorAdapter {
    private final List<SignSecure> signSecures;
    private static final String TIMESTAMP = "timestamp";
    private static final String NONCE = "nonce";
    private static final String SIGNATURE = "signature";
    private static final String SHA1 = "sha1";
    private static final String MD5 = "md5";
    private static final Logger log = LoggerFactory.getLogger(SignInterceptor.class);
    private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();
    private static final Integer SECOND_MIN = 0;
    private static final Integer SECOND_MAX = 10;
 
    public SignInterceptor(final List<SignSecure> signSecures) {
        this.signSecures = signSecures;
    }
 
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) {
        boolean check = ((Boolean) this.signSecures.stream().filter(signSecure -> {
            return checkAuth(request, signSecure);
        }).findFirst().map(authSecure -> {
            return Boolean.valueOf(checkSign(authSecure.getCrypto()));
        }).orElse(Boolean.TRUE)).booleanValue();
        if (!check) {
            log.warn("授权认证失败,请求接口:{},请求IP:{},请求参数:{}", new Object[]{request.getRequestURI(), WebUtil.getIP(request), JsonUtil.toJson(request.getParameterMap())});
            ResponseProvider.write(response);
            return false;
        }
        return true;
    }
 
    private boolean checkAuth(HttpServletRequest request, SignSecure signSecure) {
        return checkMethod(request, signSecure.getMethod()) && checkPath(request, signSecure.getPattern());
    }
 
    private boolean checkMethod(HttpServletRequest request, HttpMethod method) {
        return method == HttpMethod.ALL || (method != null && method == HttpMethod.of(request.getMethod()));
    }
 
    private boolean checkPath(HttpServletRequest request, String pattern) {
        String servletPath = request.getServletPath();
        String pathInfo = request.getPathInfo();
        if (pathInfo != null && pathInfo.length() > 0) {
            servletPath = servletPath + pathInfo;
        }
        return ANT_PATH_MATCHER.match(pattern, servletPath);
    }
 
    private boolean checkSign(String crypto) {
        String sign;
        try {
            HttpServletRequest request = WebUtil.getRequest();
            if (request == null) {
                return false;
            }
            String timestamp = request.getHeader(TIMESTAMP);
            long seconds = Duration.between(new Date(Func.toLong(timestamp)).toInstant(), DateUtil.now().toInstant()).getSeconds();
            if (seconds < SECOND_MIN.intValue() || seconds > SECOND_MAX.intValue()) {
                log.warn("授权认证失败,错误信息:{}", "请求时间戳非法");
                return false;
            }
            String nonce = request.getHeader(NONCE);
            String signature = request.getHeader(SIGNATURE);
            if (crypto.equals(MD5)) {
                sign = DigestUtil.md5Hex(timestamp + nonce);
            } else if (crypto.equals(SHA1)) {
                sign = DigestUtil.sha1Hex(timestamp + nonce);
            } else {
                sign = DigestUtil.sha1Hex(timestamp + nonce);
            }
            return sign.equalsIgnoreCase(signature);
        } catch (Exception e) {
            log.warn("授权认证失败,错误信息:{}", e.getMessage());
            return false;
        }
    }
}