Parcourir la source

全局过滤器跳过指定路由

sunxbiao il y a 1 an
Parent
commit
1cab9f557b

+ 22 - 0
gateWay_server/src/main/java/com/bjtdba/gateWay/filter/ApiRequestFilter.java

@@ -17,6 +17,7 @@ import org.springframework.http.HttpStatus;
17 17
 import org.springframework.http.server.reactive.ServerHttpRequest;
18 18
 import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
19 19
 import org.springframework.stereotype.Component;
20
+import org.springframework.util.AntPathMatcher;
20 21
 import org.springframework.util.MultiValueMap;
21 22
 import org.springframework.web.server.ServerWebExchange;
22 23
 import reactor.core.publisher.Flux;
@@ -24,6 +25,7 @@ import reactor.core.publisher.Mono;
24 25
 
25 26
 import java.net.URI;
26 27
 import java.nio.charset.StandardCharsets;
28
+import java.util.Collections;
27 29
 import java.util.List;
28 30
 
29 31
 @Component
@@ -35,6 +37,20 @@ public class ApiRequestFilter implements GlobalFilter, Ordered {
35 37
 
36 38
     JSONObject jsonObject=new JSONObject();
37 39
 
40
+    private final AntPathMatcher pathMatcher = new AntPathMatcher();
41
+
42
+    private static final List<String> EXTERNAL_PATHS = Collections.singletonList(
43
+            "/external/**"
44
+    );
45
+
46
+    private boolean isExternalPath(String path) {
47
+        for (String pattern : EXTERNAL_PATHS) {
48
+            if (pathMatcher.match(pattern, path)) {
49
+                return true;
50
+            }
51
+        }
52
+        return false;
53
+    }
38 54
 
39 55
     @Override
40 56
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
@@ -47,6 +63,12 @@ public class ApiRequestFilter implements GlobalFilter, Ordered {
47 63
         String path = request.getPath().value();
48 64
         String method = request.getMethodValue();
49 65
         HttpHeaders header = request.getHeaders();
66
+
67
+        // 检查是否为外部API路径
68
+        if (isExternalPath(path)) {
69
+            return chain.filter(exchange); // 跳过当前过滤器
70
+        }
71
+
50 72
         log.info("***********************************请求信息**********************************");
51 73
 
52 74
         jsonObject.set("URI",URIPath);

+ 21 - 1
gateWay_server/src/main/java/com/bjtdba/gateWay/filter/LoginFilter.java

@@ -5,11 +5,13 @@ import org.springframework.cloud.gateway.filter.GlobalFilter;
5 5
 import org.springframework.core.Ordered;
6 6
 import org.springframework.http.HttpHeaders;
7 7
 import org.springframework.stereotype.Component;
8
+import org.springframework.util.AntPathMatcher;
8 9
 import org.springframework.web.server.ServerWebExchange;
9 10
 import reactor.core.publisher.Mono;
10 11
 
11 12
 import java.text.ParseException;
12 13
 import java.text.SimpleDateFormat;
14
+import java.util.Collections;
13 15
 import java.util.Date;
14 16
 import java.util.List;
15 17
 
@@ -22,6 +24,11 @@ import static com.bjtdba.utils.DESUtil.decrypt;
22 24
 @Component
23 25
 public class LoginFilter implements GlobalFilter, Ordered {
24 26
 
27
+    private final AntPathMatcher pathMatcher = new AntPathMatcher();
28
+
29
+    private static final List<String> EXTERNAL_PATHS = Collections.singletonList(
30
+            "/external/**"
31
+    );
25 32
     /**
26 33
      * 业务方法用于加密
27 34
      * 1、
@@ -30,6 +37,12 @@ public class LoginFilter implements GlobalFilter, Ordered {
30 37
      * @return
31 38
      */
32 39
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
40
+        String path = exchange.getRequest().getURI().getPath();
41
+        // 检查是否为外部API路径
42
+        if (isExternalPath(path)) {
43
+            return chain.filter(exchange); // 跳过当前过滤器
44
+        }
45
+
33 46
         HttpHeaders headers =  exchange.getRequest().getHeaders();
34 47
         List<String> list = headers.get("volunteerdes");
35 48
         if(null != list){
@@ -60,7 +73,14 @@ public class LoginFilter implements GlobalFilter, Ordered {
60 73
         return chain.filter(exchange);
61 74
     }
62 75
 
63
-
76
+    private boolean isExternalPath(String path) {
77
+        for (String pattern : EXTERNAL_PATHS) {
78
+            if (pathMatcher.match(pattern, path)) {
79
+                return true;
80
+            }
81
+        }
82
+        return false;
83
+    }
64 84
 
65 85
     /**
66 86
      * 过滤方法登记

+ 25 - 0
gateWay_server/src/main/java/com/bjtdba/gateWay/filter/WrapperResponseGlobalFilter.java

@@ -18,11 +18,14 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
18 18
 import org.springframework.http.server.reactive.ServerHttpResponse;
19 19
 import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
20 20
 import org.springframework.stereotype.Component;
21
+import org.springframework.util.AntPathMatcher;
21 22
 import org.springframework.web.server.ServerWebExchange;
22 23
 import reactor.core.publisher.Flux;
23 24
 import reactor.core.publisher.Mono;
24 25
 
25 26
 import java.nio.charset.Charset;
27
+import java.util.Collections;
28
+import java.util.List;
26 29
 
27 30
 /**
28 31
  * @Date 2022/12/1
@@ -34,8 +37,30 @@ import java.nio.charset.Charset;
34 37
 @Slf4j
35 38
 public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
36 39
 
40
+    private final AntPathMatcher pathMatcher = new AntPathMatcher();
41
+
42
+    private static final List<String> EXTERNAL_PATHS = Collections.singletonList(
43
+            "/external/**"
44
+    );
45
+
46
+    private boolean isExternalPath(String path) {
47
+        for (String pattern : EXTERNAL_PATHS) {
48
+            if (pathMatcher.match(pattern, path)) {
49
+                return true;
50
+            }
51
+        }
52
+        return false;
53
+    }
54
+
37 55
     @Override
38 56
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
57
+
58
+        String path = exchange.getRequest().getURI().getPath();
59
+        // 检查是否为外部API路径
60
+        if (isExternalPath(path)) {
61
+            return chain.filter(exchange); // 跳过当前过滤器
62
+        }
63
+
39 64
         //获取response的 返回数据
40 65
         ServerHttpRequest originalRequest = exchange.getRequest();
41 66
         ServerHttpResponse originalResponse = exchange.getResponse();