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
package com.qianwen.core.secure.interceptor;
 
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.auth.AuthFun;
import com.qianwen.core.secure.props.AuthSecure;
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.WebUtil;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
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/AuthInterceptor.class */
public class AuthInterceptor extends HandlerInterceptorAdapter {
    private static final Logger log = LoggerFactory.getLogger(AuthInterceptor.class);
    private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();
    private static final EvaluationContext EVALUATION_CONTEXT = new StandardEvaluationContext(new AuthFun());
    private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();
    private final List<AuthSecure> authSecures;
 
    public AuthInterceptor(final List<AuthSecure> authSecures) {
        this.authSecures = authSecures;
    }
 
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) {
        boolean check = ((Boolean) this.authSecures.stream().filter(authSecure -> {
            return checkAuth(request, authSecure);
        }).findFirst().map(authSecure2 -> {
            return Boolean.valueOf(checkExpression(authSecure2.getExpression()));
        }).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, AuthSecure authSecure) {
        return checkMethod(request, authSecure.getMethod()) && checkPath(request, authSecure.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 checkExpression(String expression) {
        Boolean result = (Boolean) EXPRESSION_PARSER.parseExpression(expression).getValue(EVALUATION_CONTEXT, Boolean.class);
        if (result != null) {
            return result.booleanValue();
        }
        return false;
    }
}