Skip to content

Commit 034022b

Browse files
authored
Merge pull request #2984 from Guofuyinan/main
Support setting Spring.webflux.base-path and supporting it in path predicate Fixes #1759
2 parents 85cd063 + bd8c3cc commit 034022b

File tree

7 files changed

+113
-11
lines changed

7 files changed

+113
-11
lines changed

docs/modules/ROOT/pages/spring-cloud-gateway/request-predicates-factories.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ This route matches if the request path was, for example: `/red/1` or `/red/1/` o
192192

193193
If `matchTrailingSlash` is set to `false`, then request path `/red/1/` will not be matched.
194194

195+
If you have set `spring.webflux.base-path` property, this will influence the path matching. The property value will be automatically prepended to the path patterns. For example, with `spring.webflux.base-path=/app` and a path pattern of `/red/{segment}`, the full pattern used for matching would be `/app/red/{segment}`.
196+
195197
This predicate extracts the URI template variables (such as `segment`, defined in the preceding example) as a map of names and values and places it in the `ServerWebExchange.getAttributes()` with a key defined in `ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE`.
196198
Those values are then available for use by <<gateway-route-filters,`GatewayFilter` factories>>
197199

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer;
5757
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
5858
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
59+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
5960
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6061
import org.springframework.boot.context.properties.PropertyMapper;
6162
import org.springframework.boot.ssl.SslBundles;
@@ -190,6 +191,7 @@
190191
* @author Mete Alpaslan Katırcıoğlu
191192
* @author Alberto C. Ríos
192193
* @author Olga Maciaszek-Sharma
194+
* @author FuYiNan Guo
193195
*/
194196
@Configuration(proxyBeanMethods = false)
195197
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@@ -471,8 +473,8 @@ public MethodRoutePredicateFactory methodRoutePredicateFactory() {
471473

472474
@Bean
473475
@ConditionalOnEnabledPredicate
474-
public PathRoutePredicateFactory pathRoutePredicateFactory() {
475-
return new PathRoutePredicateFactory();
476+
public PathRoutePredicateFactory pathRoutePredicateFactory(WebFluxProperties webFluxProperties) {
477+
return new PathRoutePredicateFactory(webFluxProperties);
476478
}
477479

478480
@Bean

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactory.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626

27+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
2728
import org.springframework.core.style.ToStringCreator;
2829
import org.springframework.http.server.PathContainer;
30+
import org.springframework.util.StringUtils;
2931
import org.springframework.web.server.ServerWebExchange;
3032
import org.springframework.web.util.pattern.PathPattern;
3133
import org.springframework.web.util.pattern.PathPattern.PathMatchInfo;
@@ -41,6 +43,7 @@
4143
/**
4244
* @author Spencer Gibb
4345
* @author Dhawal Kapil
46+
* @author FuYiNan Guo
4447
*/
4548
public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<PathRoutePredicateFactory.Config> {
4649

@@ -50,8 +53,20 @@ public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<Pat
5053

5154
private PathPatternParser pathPatternParser = new PathPatternParser();
5255

56+
private final WebFluxProperties webFluxProperties;
57+
58+
/**
59+
* @deprecated {@link #PathRoutePredicateFactory(WebFluxProperties)}
60+
*/
61+
@Deprecated
5362
public PathRoutePredicateFactory() {
5463
super(Config.class);
64+
this.webFluxProperties = new WebFluxProperties();
65+
}
66+
67+
public PathRoutePredicateFactory(WebFluxProperties webFluxProperties) {
68+
super(Config.class);
69+
this.webFluxProperties = webFluxProperties;
5570
}
5671

5772
private static void traceMatch(String prefix, Object desired, Object actual, boolean match) {
@@ -82,7 +97,16 @@ public Predicate<ServerWebExchange> apply(Config config) {
8297
synchronized (this.pathPatternParser) {
8398
pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchTrailingSlash());
8499
config.getPatterns().forEach(pattern -> {
85-
PathPattern pathPattern = this.pathPatternParser.parse(pattern);
100+
String basePath = webFluxProperties.getBasePath();
101+
boolean basePathIsNotBlank = StringUtils.hasText(basePath);
102+
String pathPatternStr = pattern;
103+
if (basePathIsNotBlank) {
104+
if (pattern.length() > 1 && !pattern.startsWith("/")) {
105+
basePath += ("/");
106+
}
107+
pathPatternStr = basePath + pattern;
108+
}
109+
PathPattern pathPattern = this.pathPatternParser.parse(pathPatternStr);
86110
pathPatterns.add(pathPattern);
87111
});
88112
}

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/GatewayPredicateVisitorTests.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,41 @@
1616

1717
package org.springframework.cloud.gateway.handler.predicate;
1818

19+
import java.net.URI;
1920
import java.util.ArrayList;
21+
import java.util.LinkedHashSet;
22+
import java.util.List;
2023
import java.util.function.Predicate;
2124

2225
import org.junit.jupiter.api.Test;
26+
import org.mockito.ArgumentCaptor;
27+
import reactor.core.publisher.Mono;
2328

29+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
30+
import org.springframework.cloud.gateway.filter.GatewayFilter;
31+
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
32+
import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
2433
import org.springframework.cloud.gateway.handler.AsyncPredicate;
2534
import org.springframework.cloud.gateway.route.Route;
35+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
36+
import org.springframework.mock.web.server.MockServerWebExchange;
2637
import org.springframework.web.server.ServerWebExchange;
2738

2839
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.mockito.Mockito.mock;
41+
import static org.mockito.Mockito.when;
42+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
43+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
2944

3045
/**
3146
* @author Spencer Gibb
47+
* @author FuYiNan Guo
3248
*/
3349
public class GatewayPredicateVisitorTests {
3450

3551
@Test
3652
public void asyncPredicateVisitVisitsEachNode() {
37-
PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory();
53+
PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory(new WebFluxProperties());
3854
HostRoutePredicateFactory hostRoutePredicateFactory = new HostRoutePredicateFactory();
3955
ReadBodyRoutePredicateFactory readBodyRoutePredicateFactory1 = new ReadBodyRoutePredicateFactory();
4056
ReadBodyRoutePredicateFactory readBodyRoutePredicateFactory2 = new ReadBodyRoutePredicateFactory();
@@ -55,7 +71,7 @@ public void asyncPredicateVisitVisitsEachNode() {
5571

5672
@Test
5773
public void predicateVisitVisitsEachNode() {
58-
PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory();
74+
PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory(new WebFluxProperties());
5975
HostRoutePredicateFactory hostRoutePredicateFactory = new HostRoutePredicateFactory();
6076
Predicate<ServerWebExchange> predicate = pathRoutePredicateFactory.apply(pathRoutePredicateFactory.newConfig())
6177
.and(hostRoutePredicateFactory.apply(hostRoutePredicateFactory.newConfig()));
@@ -68,4 +84,59 @@ public void predicateVisitVisitsEachNode() {
6884
.hasExactlyElementsOfTypes(PathRoutePredicateFactory.Config.class, HostRoutePredicateFactory.Config.class);
6985
}
7086

87+
@Test
88+
public void pathRoutePredicateVisitWithSetWebfluxBasePath() {
89+
WebFluxProperties webFluxProperties = new WebFluxProperties();
90+
webFluxProperties.setBasePath("/gw/api/v1");
91+
92+
PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory(webFluxProperties);
93+
PathRoutePredicateFactory.Config config = new PathRoutePredicateFactory.Config()
94+
.setPatterns(List.of("/temp/**"))
95+
.setMatchTrailingSlash(true);
96+
97+
Predicate<ServerWebExchange> predicate = pathRoutePredicateFactory.apply(config);
98+
99+
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://127.0.0.1:8080/gw/api/v1/temp/test")
100+
.build());
101+
102+
assertThat(predicate.test(exchange)).isEqualTo(true);
103+
}
104+
105+
@Test
106+
public void pathRoutePredicateVisitWithSetWebfluxBasePathStripPrefix() {
107+
WebFluxProperties webFluxProperties = new WebFluxProperties();
108+
webFluxProperties.setBasePath("/gw/api/v1");
109+
110+
PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory(webFluxProperties);
111+
PathRoutePredicateFactory.Config config = new PathRoutePredicateFactory.Config()
112+
.setPatterns(List.of("/temp/**"))
113+
.setMatchTrailingSlash(true);
114+
115+
Predicate<ServerWebExchange> predicate = pathRoutePredicateFactory.apply(config);
116+
117+
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://127.0.0.1:8080/gw/api/v1/temp/test")
118+
.build());
119+
120+
assertThat(predicate.test(exchange)).isEqualTo(true);
121+
122+
// webflux base path strips prefix is 3
123+
GatewayFilter filter = new StripPrefixGatewayFilterFactory().apply(c -> c.setParts(3));
124+
125+
GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
126+
127+
ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
128+
when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
129+
130+
filter.filter(exchange, filterChain);
131+
132+
ServerWebExchange webExchange = captor.getValue();
133+
134+
assertThat(webExchange.getRequest().getURI()).hasPath("/temp/test");
135+
136+
URI requestUrl = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
137+
assertThat(requestUrl).hasScheme("http").hasHost("127.0.0.1").hasPort(8080).hasPath("/temp/test");
138+
139+
LinkedHashSet<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
140+
assertThat(uris).contains(exchange.getRequest().getURI());
141+
}
71142
}

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactoryTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.beans.factory.config.BeanPostProcessor;
2727
import org.springframework.boot.SpringBootConfiguration;
2828
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
2930
import org.springframework.boot.test.context.SpringBootTest;
3031
import org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping;
3132
import org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory.Config;
@@ -133,14 +134,14 @@ public void matchOptionalTrailingSeparatorCopiedToMatchTrailingSlash() {
133134
@Test
134135
public void toStringFormat() {
135136
Config config = new Config().setPatterns(Arrays.asList("patternA", "patternB")).setMatchTrailingSlash(false);
136-
Predicate predicate = new PathRoutePredicateFactory().apply(config);
137+
Predicate predicate = new PathRoutePredicateFactory(new WebFluxProperties()).apply(config);
137138
assertThat(predicate.toString()).contains("patternA").contains("patternB").contains("false");
138139
}
139140

140141
@Test
141142
public void toStringFormatMatchTrailingSlashTrue() {
142143
Config config = new Config().setPatterns(Arrays.asList("patternA", "patternB")).setMatchTrailingSlash(true);
143-
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory().apply(config);
144+
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory(new WebFluxProperties()).apply(config);
144145
assertThat(predicate.toString()).contains("patternA").contains("patternB").contains("true");
145146
}
146147

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicatePathContainerAttrBenchMarkTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.openjdk.jmh.annotations.Threads;
3131
import org.openjdk.jmh.annotations.Warmup;
3232

33+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
3334
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
3435
import org.springframework.mock.web.server.MockServerWebExchange;
3536
import org.springframework.web.server.ServerWebExchange;
@@ -54,7 +55,7 @@ public class PathRoutePredicatePathContainerAttrBenchMarkTests {
5455
PathRoutePredicateFactory.Config config = new PathRoutePredicateFactory.Config()
5556
.setPatterns(Collections.singletonList(PATH_PATTERN_PREFIX + i))
5657
.setMatchTrailingSlash(true);
57-
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory().apply(config);
58+
Predicate<ServerWebExchange> predicate = new PathRoutePredicateFactory(new WebFluxProperties()).apply(config);
5859
predicates.add(predicate);
5960
}
6061
}

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/tagsprovider/GatewayPathTagsProviderTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.micrometer.core.instrument.Tags;
2323
import org.junit.jupiter.api.Test;
2424

25+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
2526
import org.springframework.cloud.gateway.handler.predicate.HostRoutePredicateFactory;
2627
import org.springframework.cloud.gateway.handler.predicate.MethodRoutePredicateFactory;
2728
import org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory;
@@ -56,7 +57,7 @@ void addPathToRoutes() {
5657
Route route = Route.async()
5758
.id("git")
5859
.uri(ROUTE_URI)
59-
.predicate(new PathRoutePredicateFactory().apply(pathConfig)
60+
.predicate(new PathRoutePredicateFactory(new WebFluxProperties()).apply(pathConfig)
6061
.and(new HostRoutePredicateFactory().apply(hostConfig)))
6162
.build();
6263

@@ -81,8 +82,8 @@ void addsMultiplePathToRoutes() {
8182
Route route = Route.async()
8283
.id("git")
8384
.uri(ROUTE_URI)
84-
.predicate(new PathRoutePredicateFactory().apply(pathConfig)
85-
.or(new PathRoutePredicateFactory().apply(pathConfig2)))
85+
.predicate(new PathRoutePredicateFactory(new WebFluxProperties()).apply(pathConfig)
86+
.or(new PathRoutePredicateFactory(new WebFluxProperties()).apply(pathConfig2)))
8687
.build();
8788

8889
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(ROUTE_URI).build());

0 commit comments

Comments
 (0)