From dfc52c7ad19ab95b26d97ba5d660ce8b1c234874 Mon Sep 17 00:00:00 2001 From: emilnkrastev Date: Thu, 17 Jan 2019 01:27:27 +0200 Subject: [PATCH] Fix trailing slash in strip prefix filter (#770) The existing Strip Prefix filter removes the trailing slash, which causes incoming requests to fail upon hitting the downstream service. --- .../gateway/filter/factory/StripPrefixGatewayFilterFactory.java | 2 ++ .../filter/factory/StripPrefixGatewayFilterFactoryTests.java | 2 ++ 2 files changed, 4 insertions(+) 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);