瀏覽代碼

解决excludePathPatterns不生效问题

xiehui 3 年之前
父節點
當前提交
77838aadad

+ 1 - 0
spring-wechars/src/main/java/cn/org/spring/wechar/config/BasicInfo.java

@@ -21,4 +21,5 @@ public class BasicInfo {
      * 项目前缀
      */
     private String urlPrefix = "http://xhccl.iok.la";
+
 }

+ 11 - 0
spring-wechars/src/main/java/cn/org/spring/wechar/config/MessageConfig.java

@@ -9,6 +9,7 @@ import cn.org.spring.wechar.service.message.TextMessageService;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.util.AntPathMatcher;
 
 /**
  * 消息配置类
@@ -32,4 +33,14 @@ public class MessageConfig {
         return contextMessageFactory;
     }
 
+    /**
+     * 注入一个字符匹配工具类,由spring提供
+     *
+     * @return
+     */
+    @Bean
+    public AntPathMatcher getAntPathMatcher() {
+        return new AntPathMatcher();
+    }
+
 }

+ 30 - 39
spring-wechars/src/main/java/cn/org/spring/wechar/config/WebConfig.java

@@ -1,5 +1,13 @@
 package cn.org.spring.wechar.config;
 
+import cn.org.spring.wechar.interceptor.LoginUserInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
 /**
  * @author: 谢辉
  * @date: 2021/5/10
@@ -9,42 +17,25 @@ package cn.org.spring.wechar.config;
  * @version: 1.0
  */
 
-//@Configuration
-//public class WebConfig implements WebMvcConfigurer {
-//
-//    @Autowired
-//    private LoginUserInterceptor loginUserInterceptor;
-//
-//    @Override
-//    public void addInterceptors(InterceptorRegistry registry) {
-//        //注册TestInterceptor拦截器
-//        InterceptorRegistration registration = registry.addInterceptor(loginUserInterceptor);
-//        System.out.println("loginUserInterceptor:" + loginUserInterceptor);
-//        registration.addPathPatterns("/**");
-//        registration.excludePathPatterns("/weChar");
-//        registration.excludePathPatterns("/getCode");
-//        registration.excludePathPatterns("/index");
-//        registration.excludePathPatterns("/index.html");
-//        registration.excludePathPatterns("/login");
-//        registration.excludePathPatterns("/login.html");
-//        registration.excludePathPatterns(
-//                "/**/*.html",
-//                "/**/*.js",
-//                "/**/*.css",
-//                "/**/*.woff",
-//                "/**/*.ttf"
-//        );
-//    }
-//
-//
-//    @Override
-//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
-//        /*
-//         * addResourceHandler是访问路径前缀,参数可设置多个,以逗号分隔,例:/image/**,/css/**
-//         * addResourceLocations是对应的资源路径,也可以设置多个。
-//         */
-//        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
-//        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
-//    }
-//
-//}
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+
+    @Autowired
+    private LoginUserInterceptor loginUserInterceptor;
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        //注册TestInterceptor拦截器
+        InterceptorRegistration registration = registry.addInterceptor(loginUserInterceptor);
+        System.out.println("loginUserInterceptor:" + loginUserInterceptor);
+        registration.addPathPatterns("/**");
+    }
+
+
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
+        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
+    }
+
+}

+ 41 - 2
spring-wechars/src/main/java/cn/org/spring/wechar/interceptor/LoginUserInterceptor.java

@@ -1,5 +1,22 @@
 package cn.org.spring.wechar.interceptor;
 
+import cn.org.spring.wechar.bean.Result;
+import cn.org.spring.wechar.config.BasicInfo;
+import cn.org.spring.wechar.utils.CacheUtil;
+import cn.org.spring.wechar.utils.CookieUtils;
+import com.alibaba.fastjson.JSONObject;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+import org.springframework.util.AntPathMatcher;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.PrintWriter;
+
 /**
  * @author: 谢辉
  * @date: 2021/5/10
@@ -8,9 +25,11 @@ package cn.org.spring.wechar.interceptor;
  * @modifiedBy:
  * @version: 1.0
  */
-/*
+
 @Slf4j
+@Data
 @Component
+@ConfigurationProperties(prefix = "login-interceptor")
 public class LoginUserInterceptor extends HandlerInterceptorAdapter {
     @Autowired
     CacheUtil cacheUtil;
@@ -18,8 +37,28 @@ public class LoginUserInterceptor extends HandlerInterceptorAdapter {
     @Autowired
     BasicInfo basicInfo;
 
+    @Autowired
+    AntPathMatcher antPathMatcher;
+
+    private String excludePatterns = "";
+
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        String uri = request.getRequestURI();
+
+        System.out.println("request.getRequestURI():" + uri);
+        System.out.println("不拦截路径信息:" + excludePatterns);
+
+        String[] split = excludePatterns.split(",");
+        for (String s : split) {
+            boolean match = antPathMatcher.match(s, uri);
+            if (match) {
+                log.info("匹配到不拦截的路径:【{}】", s);
+                return true;
+            }
+        }
+
+
         String cookieValue = CookieUtils.getCookieValue(request, "wechat-token");
         Object o = cacheUtil.get("wechatuser:" + cookieValue);
         log.info("LoginUserInterceptor拦截器从redis获取信息【{}】", String.valueOf(o));
@@ -49,5 +88,5 @@ public class LoginUserInterceptor extends HandlerInterceptorAdapter {
     public static boolean isAjax(HttpServletRequest request) {
         return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
     }
-}*/
+}
 

+ 6 - 0
spring-wechars/src/main/resources/application.yml

@@ -49,5 +49,11 @@ basicinfo:
   url-prefix: http://xhccl.iok.la
 
 
+
 logging:
   config: classpath:logback-spring.xml
+
+#登录拦截器设置放行路径,多个用,号隔开
+login-interceptor:
+  exclude-patterns: /**/weChar,/**/weChar/getCode,/**/index,/**/login,/**/*.html,/**/*.js,/**/*.css
+