diff --git a/.gitignore b/.gitignore index 62227c8c64..42eea8769f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ _site/ *.swo .vscode/ .flattened-pom.xml +.devcontainer/ diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java index 541568ae47..b232282493 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/StripPrefixGatewayFilterFactory.java @@ -79,8 +79,11 @@ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { newPath.append('/'); } - ServerHttpRequest newRequest = request.mutate().path(newPath.toString()).build(); - + ServerHttpRequest.Builder requestBuilder = request.mutate().path(newPath.toString()); + if (!(config.getEnforceContextPath())) { + requestBuilder.contextPath(null); + } + ServerHttpRequest newRequest = requestBuilder.build(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI()); return chain.filter(exchange.mutate().request(newRequest).build()); @@ -89,7 +92,7 @@ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { @Override public String toString() { return filterToStringCreator(StripPrefixGatewayFilterFactory.this).append("parts", config.getParts()) - .toString(); + .append("enforeContextPath", config.getEnforceContextPath()).toString(); } }; } @@ -98,6 +101,8 @@ public static class Config { private int parts = 1; + private boolean enforceContextPath = true; + public int getParts() { return parts; } @@ -106,6 +111,14 @@ public void setParts(int parts) { this.parts = parts; } + public boolean getEnforceContextPath() { + return enforceContextPath; + } + + public void setEnforceContextPath(boolean enforceContextPath) { + this.enforceContextPath = enforceContextPath; + } + } } diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java index d9c2a6af95..eb1ee2701f 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java @@ -742,6 +742,20 @@ public GatewayFilterSpec stripPrefix(int parts) { return filter(getBean(StripPrefixGatewayFilterFactory.class).apply(c -> c.setParts(parts))); } + /** + * Strips the prefix from the path of the request before it is routed by the Gateway. + * @param parts the number of parts of the path to remove + * @param enforeContextPath whether the resulting path must contain the context-path + * defined for the application + * @return a {@link GatewayFilterSpec} that can be used to apply additional filters + */ + public GatewayFilterSpec stripPrefix(int parts, boolean enforceContextPath) { + return filter(getBean(StripPrefixGatewayFilterFactory.class).apply(c -> { + c.setParts(parts); + c.setEnforceContextPath(enforceContextPath); + })); + } + /** * A filter which changes the URI the request will be routed to by the Gateway by * pulling it from a header on the request.