diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java index 949ec238..a97309a1 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java @@ -53,6 +53,7 @@ public class StripPrefixGatewayFilterFactory extends AbstractGatewayFilterFactor String path = request.getURI().getRawPath(); String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(path, "/")) .skip(config.parts).collect(Collectors.joining("/")); + newPath += (newPath.length() > 1 && path.endsWith("/") ? "/" : ""); ServerHttpRequest newRequest = request.mutate() .path(newPath) .build(); @@ -74,4 +75,5 @@ public class StripPrefixGatewayFilterFactory extends AbstractGatewayFilterFactor this.parts = parts; } } + } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactoryTests.java index 57818f48..0be5c60d 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactoryTests.java @@ -45,6 +45,8 @@ public class StripPrefixGatewayFilterFactoryTests { testStripPrefixFilter("/foo/bar", "/bar", 1); testStripPrefixFilter("/foo/bar", "/", 2); testStripPrefixFilter("/foo/bar", "/foo/bar", 0); + testStripPrefixFilter("/foo/bar/", "/", 2); + testStripPrefixFilter("/foo/bar/", "/foo/bar/", 0); testStripPrefixFilter("", "/", 1); testStripPrefixFilter("/", "/", 1); testStripPrefixFilter("/", "/", 2);