Skip to content

Commit 10eca38

Browse files
committed
Extract variables
1 parent cf916f3 commit 10eca38

File tree

6 files changed

+108
-18
lines changed

6 files changed

+108
-18
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer;
5555
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
5656
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
57+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
5758
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5859
import org.springframework.boot.context.properties.PropertyMapper;
5960
import org.springframework.boot.ssl.SslBundles;
@@ -189,13 +190,14 @@
189190
* @author Alberto C. Ríos
190191
* @author Olga Maciaszek-Sharma
191192
* @author Dominic Niemann
193+
* @author Guo FuYiNan
192194
*/
193195
@Configuration(proxyBeanMethods = false)
194196
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
195197
@EnableConfigurationProperties
196-
@AutoConfigureBefore({ HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class })
197-
@AutoConfigureAfter({ GatewayReactiveLoadBalancerClientAutoConfiguration.class,
198-
GatewayClassPathWarningAutoConfiguration.class })
198+
@AutoConfigureBefore({HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class})
199+
@AutoConfigureAfter({GatewayReactiveLoadBalancerClientAutoConfiguration.class,
200+
GatewayClassPathWarningAutoConfiguration.class})
199201
@ConditionalOnClass(DispatcherHandler.class)
200202
public class GatewayAutoConfiguration {
201203

@@ -468,8 +470,8 @@ public MethodRoutePredicateFactory methodRoutePredicateFactory() {
468470

469471
@Bean
470472
@ConditionalOnEnabledPredicate
471-
public PathRoutePredicateFactory pathRoutePredicateFactory() {
472-
return new PathRoutePredicateFactory();
473+
public PathRoutePredicateFactory pathRoutePredicateFactory(WebFluxProperties webFluxProperties) {
474+
return new PathRoutePredicateFactory(webFluxProperties);
473475
}
474476

475477
@Bean
@@ -623,7 +625,7 @@ public PrincipalNameKeyResolver principalNameKeyResolver() {
623625
}
624626

625627
@Bean
626-
@ConditionalOnBean({ RateLimiter.class, KeyResolver.class })
628+
@ConditionalOnBean({RateLimiter.class, KeyResolver.class})
627629
@ConditionalOnEnabledFilter
628630
public RequestRateLimiterGatewayFilterFactory requestRateLimiterGatewayFilterFactory(RateLimiter rateLimiter,
629631
KeyResolver resolver) {
@@ -763,7 +765,7 @@ public HttpClientSslConfigurer httpClientSslConfigurer(ServerProperties serverPr
763765
}
764766

765767
@Bean
766-
@ConditionalOnMissingBean({ HttpClient.class, HttpClientFactory.class })
768+
@ConditionalOnMissingBean({HttpClient.class, HttpClientFactory.class})
767769
public HttpClientFactory gatewayHttpClientFactory(HttpClientProperties properties,
768770
ServerProperties serverProperties, List<HttpClientCustomizer> customizers,
769771
HttpClientSslConfigurer sslConfigurer) {
@@ -866,7 +868,7 @@ static class VerboseDisabled {
866868

867869
@Configuration(proxyBeanMethods = false)
868870
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
869-
@ConditionalOnClass({ OAuth2AuthorizedClient.class, SecurityWebFilterChain.class, SecurityProperties.class })
871+
@ConditionalOnClass({OAuth2AuthorizedClient.class, SecurityWebFilterChain.class, SecurityProperties.class})
870872
@ConditionalOnEnabledFilter(TokenRelayGatewayFilterFactory.class)
871873
protected static class TokenRelayConfiguration {
872874

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

Lines changed: 16 additions & 2 deletions
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 Guo FuYiNan
4447
*/
4548
public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<PathRoutePredicateFactory.Config> {
4649

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

5154
private PathPatternParser pathPatternParser = new PathPatternParser();
5255

53-
public PathRoutePredicateFactory() {
56+
private final WebFluxProperties webFluxProperties;
57+
58+
public PathRoutePredicateFactory(WebFluxProperties webFluxProperties) {
5459
super(Config.class);
60+
this.webFluxProperties = webFluxProperties;
5561
}
5662

5763
private static void traceMatch(String prefix, Object desired, Object actual, boolean match) {
@@ -82,7 +88,15 @@ public Predicate<ServerWebExchange> apply(Config config) {
8288
synchronized (this.pathPatternParser) {
8389
pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchTrailingSlash());
8490
config.getPatterns().forEach(pattern -> {
85-
PathPattern pathPattern = this.pathPatternParser.parse(pattern);
91+
String basePath = webFluxProperties.getBasePath();
92+
boolean basePathIsNotBlank = StringUtils.hasText(basePath);
93+
if (basePathIsNotBlank) {
94+
if (pattern.length() > 1 && !pattern.startsWith("/")) {
95+
basePath += ("/");
96+
}
97+
}
98+
String pathPatternStr = basePathIsNotBlank ? basePath + pattern : pattern;
99+
PathPattern pathPattern = this.pathPatternParser.parse(pathPatternStr);
86100
pathPatterns.add(pathPattern);
87101
});
88102
}

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 Guo FuYiNan
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)