Browse Source

Add support for optionally matching trailing / in path predicate. Fixes #511

pull/545/head
Ryan Baxter 6 years ago
parent
commit
afe639d844
  1. 16
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactory.java
  2. 13
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/PredicateSpec.java
  3. 6
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactoryTests.java
  4. 2
      spring-cloud-gateway-core/src/test/resources/application.yml

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

@ -17,6 +17,7 @@
package org.springframework.cloud.gateway.handler.predicate; package org.springframework.cloud.gateway.handler.predicate;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -40,6 +41,7 @@ import static org.springframework.http.server.PathContainer.parsePath;
*/ */
public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<PathRoutePredicateFactory.Config> { public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<PathRoutePredicateFactory.Config> {
private static final Log log = LogFactory.getLog(RoutePredicateFactory.class); private static final Log log = LogFactory.getLog(RoutePredicateFactory.class);
private static final String MATCH_OPTIONAL_TRAILING_SEPARATOR_KEY = "matchOptionalTrailingSeparator";
private PathPatternParser pathPatternParser = new PathPatternParser(); private PathPatternParser pathPatternParser = new PathPatternParser();
@ -53,12 +55,13 @@ public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<Pat
@Override @Override
public List<String> shortcutFieldOrder() { public List<String> shortcutFieldOrder() {
return Collections.singletonList(PATTERN_KEY); return Arrays.asList(PATTERN_KEY, MATCH_OPTIONAL_TRAILING_SEPARATOR_KEY);
} }
@Override @Override
public Predicate<ServerWebExchange> apply(Config config) { public Predicate<ServerWebExchange> apply(Config config) {
synchronized (this.pathPatternParser) { synchronized (this.pathPatternParser) {
pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchOptionalTrailingSeparator());
config.pathPattern = this.pathPatternParser.parse(config.pattern); config.pathPattern = this.pathPatternParser.parse(config.pattern);
} }
return exchange -> { return exchange -> {
@ -88,6 +91,7 @@ public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<Pat
public static class Config { public static class Config {
private String pattern; private String pattern;
private PathPattern pathPattern; private PathPattern pathPattern;
private boolean matchOptionalTrailingSeparator = true;
public String getPattern() { public String getPattern() {
return pattern; return pattern;
@ -98,10 +102,20 @@ public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<Pat
return this; return this;
} }
public boolean isMatchOptionalTrailingSeparator() {
return matchOptionalTrailingSeparator;
}
public Config setMatchOptionalTrailingSeparator(boolean matchOptionalTrailingSeparator) {
this.matchOptionalTrailingSeparator = matchOptionalTrailingSeparator;
return this;
}
@Override @Override
public String toString() { public String toString() {
return new ToStringCreator(this) return new ToStringCreator(this)
.append("pattern", pattern) .append("pattern", pattern)
.append("matchOptionalTrailingSeparator", matchOptionalTrailingSeparator)
.toString(); .toString();
} }
} }

13
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/PredicateSpec.java

@ -170,6 +170,19 @@ public class PredicateSpec extends UriSpec {
.applyAsync(c -> c.setPattern(pattern))); .applyAsync(c -> c.setPattern(pattern)));
} }
/**
* A predicate that checks if the path of the request matches the given pattern
* @param pattern the pattern to check the path against.
* The pattern is a {@link org.springframework.util.PathMatcher} pattern
* @param matchOptionalTrailingSeparator set to false if you do not want this path to match
* when there is a trailing <code>/</code>
* @return a {@link BooleanSpec} to be used to add logical operators
*/
public BooleanSpec path(String pattern, boolean matchOptionalTrailingSeparator) {
return asyncPredicate(getBean(PathRoutePredicateFactory.class)
.applyAsync(c -> c.setPattern(pattern).setMatchOptionalTrailingSeparator(matchOptionalTrailingSeparator)));
}
/** /**
* This predicate is BETA and may be subject to change in a future release. * This predicate is BETA and may be subject to change in a future release.
* A predicate that checks the contents of the request body * A predicate that checks the contents of the request body

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

@ -44,6 +44,12 @@ public class PathRoutePredicateFactoryTests extends BaseWebClientTests {
.expectStatus().isOk() .expectStatus().isOk()
.expectHeader().valueEquals(HANDLER_MAPPER_HEADER, RoutePredicateHandlerMapping.class.getSimpleName()) .expectHeader().valueEquals(HANDLER_MAPPER_HEADER, RoutePredicateHandlerMapping.class.getSimpleName())
.expectHeader().valueEquals(ROUTE_ID_HEADER, "path_test"); .expectHeader().valueEquals(ROUTE_ID_HEADER, "path_test");
//since the configuration does not allow the trailing / to match this should fail
testClient.get().uri("/abc/123/function/")
.header(HttpHeaders.HOST, "www.path.org")
.exchange()
.expectStatus().is4xxClientError();
} }
@Test @Test

2
spring-cloud-gateway-core/src/test/resources/application.yml

@ -128,7 +128,7 @@ spring:
- id: path_test - id: path_test
uri: ${test.uri} uri: ${test.uri}
predicates: predicates:
- Path=/{org}/{scope}/function - Path=/{org}/{scope}/function,false
- Host=**.path.org - Host=**.path.org
filters: filters:
- SetPath=/anything/{org}{scope} - SetPath=/anything/{org}{scope}

Loading…
Cancel
Save