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;
|
}
|
}
|