|
@@ -0,0 +1,116 @@
|
|
|
|
+package com.hb.proj.allconfig;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.Set;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.web.filter.OncePerRequestFilter;
|
|
|
|
+
|
|
|
|
+import com.hb.proj.utils.JacksonUtils;
|
|
|
|
+import com.hb.proj.utils.RespVOBuilder;
|
|
|
|
+
|
|
|
|
+import jakarta.servlet.FilterChain;
|
|
|
|
+import jakarta.servlet.ServletException;
|
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+public class APICallFilter extends OncePerRequestFilter {
|
|
|
|
+
|
|
|
|
+ private static Logger logger=LoggerFactory.getLogger(APICallFilter.class);
|
|
|
|
+
|
|
|
|
+ private static Set<String> EXCLUDE_PATHS=null;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected void initFilterBean() throws ServletException {
|
|
|
|
+ String excludePath=super.getFilterConfig().getInitParameter("excludePath");
|
|
|
|
+ if(StringUtils.isNotBlank(excludePath)) {
|
|
|
|
+ String[] ep=excludePath.split(",");
|
|
|
|
+ EXCLUDE_PATHS=Collections.unmodifiableSet(new HashSet<>(Arrays.asList(ep)));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
|
|
|
+ throws ServletException, IOException {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if(!checkAPIAuth(request)) {
|
|
|
|
+ writeToResponse(response,JacksonUtils.getJSON(RespVOBuilder.error("权限不足或登录已过期")));
|
|
|
|
+ return ;
|
|
|
|
+ }
|
|
|
|
+ filterChain.doFilter(request,response);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private boolean checkAPIAuth(HttpServletRequest request) {
|
|
|
|
+
|
|
|
|
+ String reqUri=request.getRequestURI();
|
|
|
|
+ String contextPath=request.getContextPath();
|
|
|
|
+ reqUri=reqUri.replaceFirst(contextPath, "");
|
|
|
|
+
|
|
|
|
+ if(isExcludePath(reqUri)) {
|
|
|
|
+ logger.debug("该请求地址为排除地址:"+reqUri);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String token=request.getHeader(CacheConfig.TOKEN_HEADER_NAME);
|
|
|
|
+ if(StringUtils.isBlank(token)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ AccessToken accessToken=CacheConfig.get(token);
|
|
|
|
+ if(accessToken==null) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return accessToken.verify(reqUri);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean isExcludePath(String path)
|
|
|
|
+ {
|
|
|
|
+ if(EXCLUDE_PATHS==null||EXCLUDE_PATHS.size()==0){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ for(String pth : EXCLUDE_PATHS)
|
|
|
|
+ {
|
|
|
|
+ if(path.indexOf(pth)>=0)
|
|
|
|
+ {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void writeToResponse(HttpServletResponse response,String message){
|
|
|
|
+ PrintWriter out=null;
|
|
|
|
+ try {
|
|
|
|
+ response.setContentType("text/json;charset=UTF-8");
|
|
|
|
+ out = response.getWriter();
|
|
|
|
+ out.write(message);
|
|
|
|
+
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }finally{
|
|
|
|
+ if (out != null) {
|
|
|
|
+ out.flush();
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|