Selaa lähdekoodia

全局过滤器跳过指定路由

sunxbiao 1 vuosi sitten
vanhempi
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
 import org.springframework.http.server.reactive.ServerHttpRequest;
17
 import org.springframework.http.server.reactive.ServerHttpRequest;
18
 import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
18
 import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
19
 import org.springframework.stereotype.Component;
19
 import org.springframework.stereotype.Component;
20
+import org.springframework.util.AntPathMatcher;
20
 import org.springframework.util.MultiValueMap;
21
 import org.springframework.util.MultiValueMap;
21
 import org.springframework.web.server.ServerWebExchange;
22
 import org.springframework.web.server.ServerWebExchange;
22
 import reactor.core.publisher.Flux;
23
 import reactor.core.publisher.Flux;
@@ -24,6 +25,7 @@ import reactor.core.publisher.Mono;
24
 
25
 
25
 import java.net.URI;
26
 import java.net.URI;
26
 import java.nio.charset.StandardCharsets;
27
 import java.nio.charset.StandardCharsets;
28
+import java.util.Collections;
27
 import java.util.List;
29
 import java.util.List;
28
 
30
 
29
 @Component
31
 @Component
@@ -35,6 +37,20 @@ public class ApiRequestFilter implements GlobalFilter, Ordered {
35
 
37
 
36
     JSONObject jsonObject=new JSONObject();
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
     @Override
55
     @Override
40
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
56
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
@@ -47,6 +63,12 @@ public class ApiRequestFilter implements GlobalFilter, Ordered {
47
         String path = request.getPath().value();
63
         String path = request.getPath().value();
48
         String method = request.getMethodValue();
64
         String method = request.getMethodValue();
49
         HttpHeaders header = request.getHeaders();
65
         HttpHeaders header = request.getHeaders();
66
+
67
+        // 检查是否为外部API路径
68
+        if (isExternalPath(path)) {
69
+            return chain.filter(exchange); // 跳过当前过滤器
70
+        }
71
+
50
         log.info("***********************************请求信息**********************************");
72
         log.info("***********************************请求信息**********************************");
51
 
73
 
52
         jsonObject.set("URI",URIPath);
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
 import org.springframework.core.Ordered;
5
 import org.springframework.core.Ordered;
6
 import org.springframework.http.HttpHeaders;
6
 import org.springframework.http.HttpHeaders;
7
 import org.springframework.stereotype.Component;
7
 import org.springframework.stereotype.Component;
8
+import org.springframework.util.AntPathMatcher;
8
 import org.springframework.web.server.ServerWebExchange;
9
 import org.springframework.web.server.ServerWebExchange;
9
 import reactor.core.publisher.Mono;
10
 import reactor.core.publisher.Mono;
10
 
11
 
11
 import java.text.ParseException;
12
 import java.text.ParseException;
12
 import java.text.SimpleDateFormat;
13
 import java.text.SimpleDateFormat;
14
+import java.util.Collections;
13
 import java.util.Date;
15
 import java.util.Date;
14
 import java.util.List;
16
 import java.util.List;
15
 
17
 
@@ -22,6 +24,11 @@ import static com.bjtdba.utils.DESUtil.decrypt;
22
 @Component
24
 @Component
23
 public class LoginFilter implements GlobalFilter, Ordered {
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
      * 1、
34
      * 1、
@@ -30,6 +37,12 @@ public class LoginFilter implements GlobalFilter, Ordered {
30
      * @return
37
      * @return
31
      */
38
      */
32
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
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
         HttpHeaders headers =  exchange.getRequest().getHeaders();
46
         HttpHeaders headers =  exchange.getRequest().getHeaders();
34
         List<String> list = headers.get("volunteerdes");
47
         List<String> list = headers.get("volunteerdes");
35
         if(null != list){
48
         if(null != list){
@@ -60,7 +73,14 @@ public class LoginFilter implements GlobalFilter, Ordered {
60
         return chain.filter(exchange);
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
 import org.springframework.http.server.reactive.ServerHttpResponse;
18
 import org.springframework.http.server.reactive.ServerHttpResponse;
19
 import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
19
 import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
20
 import org.springframework.stereotype.Component;
20
 import org.springframework.stereotype.Component;
21
+import org.springframework.util.AntPathMatcher;
21
 import org.springframework.web.server.ServerWebExchange;
22
 import org.springframework.web.server.ServerWebExchange;
22
 import reactor.core.publisher.Flux;
23
 import reactor.core.publisher.Flux;
23
 import reactor.core.publisher.Mono;
24
 import reactor.core.publisher.Mono;
24
 
25
 
25
 import java.nio.charset.Charset;
26
 import java.nio.charset.Charset;
27
+import java.util.Collections;
28
+import java.util.List;
26
 
29
 
27
 /**
30
 /**
28
  * @Date 2022/12/1
31
  * @Date 2022/12/1
@@ -34,8 +37,30 @@ import java.nio.charset.Charset;
34
 @Slf4j
37
 @Slf4j
35
 public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
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
     @Override
55
     @Override
38
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
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
         //获取response的 返回数据
64
         //获取response的 返回数据
40
         ServerHttpRequest originalRequest = exchange.getRequest();
65
         ServerHttpRequest originalRequest = exchange.getRequest();
41
         ServerHttpResponse originalResponse = exchange.getResponse();
66
         ServerHttpResponse originalResponse = exchange.getResponse();