-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Adds StripContextPath filter [webflux] #4129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
garvit-joshi
wants to merge
1
commit into
spring-cloud:main
Choose a base branch
from
garvit-joshi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ud-gateway-server-webflux/gatewayfilter-factories/stripcontextpath-factory.adoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| [[stripcontextpath-gatewayfilter-factory]] | ||
| = `StripContextPath` `GatewayFilter` Factory | ||
|
|
||
| The `StripContextPath` `GatewayFilter` factory removes the request context path before downstream path filters run. | ||
| This is useful when a context path is present through `spring.webflux.base-path` or forwarded prefixes and you want filters such as `StripPrefix`, `RewritePath`, `SetPath`, or `PrefixPath` to operate on the application path instead. | ||
|
|
||
| The `StripContextPath` filter must be listed before any path-manipulating filters. | ||
| The following example configures a `StripContextPath` `GatewayFilter`: | ||
|
|
||
| .application.yml | ||
| [source,yaml] | ||
| ---- | ||
| spring: | ||
| cloud: | ||
| gateway: | ||
| server: | ||
| webflux: | ||
| routes: | ||
| - id: strip_context_path_route | ||
| uri: https://example.org | ||
| predicates: | ||
| - Path=/name/** | ||
| filters: | ||
| - StripContextPath | ||
| - StripPrefix=1 | ||
| ---- | ||
|
|
||
| With a context path of `/context`, a request to `/context/name/blue` first becomes `/name/blue`, and then `StripPrefix=1` sends `https://example.org/blue` downstream. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...rg/springframework/cloud/gateway/filter/factory/StripContextPathGatewayFilterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright 2013-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.cloud.gateway.filter.factory; | ||
|
|
||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
| import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
| import org.springframework.http.server.reactive.ServerHttpRequest; | ||
| import org.springframework.util.StringUtils; | ||
| import org.springframework.web.server.ServerWebExchange; | ||
|
|
||
| import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator; | ||
| import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; | ||
| import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl; | ||
|
|
||
| /** | ||
| * Strips the request context path before later path-manipulating filters run. | ||
| * | ||
| * @author Garvit Joshi | ||
| */ | ||
| public class StripContextPathGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> { | ||
|
|
||
| public GatewayFilter apply() { | ||
| return apply(new Object()); | ||
| } | ||
|
|
||
| @Override | ||
| public GatewayFilter apply(Object config) { | ||
| return new GatewayFilter() { | ||
| @Override | ||
| public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
| ServerHttpRequest request = exchange.getRequest(); | ||
| String contextPath = request.getPath().contextPath().value(); | ||
| if (!StringUtils.hasText(contextPath)) { | ||
| return chain.filter(exchange); | ||
| } | ||
|
|
||
| addOriginalRequestUrl(exchange, request.getURI()); | ||
|
|
||
| String newPath = request.getPath().pathWithinApplication().value(); | ||
| if (!StringUtils.hasText(newPath)) { | ||
| newPath = "/"; | ||
| } | ||
|
|
||
| ServerHttpRequest newRequest = request.mutate().contextPath("").path(newPath).build(); | ||
| exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI()); | ||
|
|
||
| return chain.filter(exchange.mutate().request(newRequest).build()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return filterToStringCreator(StripContextPathGatewayFilterFactory.this).toString(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...ringframework/cloud/gateway/filter/factory/StripContextPathGatewayFilterFactoryTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Copyright 2013-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.cloud.gateway.filter.factory; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.LinkedHashSet; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
| import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
| import org.springframework.mock.http.server.reactive.MockServerHttpRequest; | ||
| import org.springframework.mock.web.server.MockServerWebExchange; | ||
| import org.springframework.web.server.ServerWebExchange; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR; | ||
| import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; | ||
|
|
||
| /** | ||
| * @author Garvit Joshi | ||
| */ | ||
| public class StripContextPathGatewayFilterFactoryTests { | ||
|
|
||
| @Test | ||
| public void stripContextPathFilterWorks() { | ||
| MockServerHttpRequest request = request("/context/service/path", "/context"); | ||
|
|
||
| ServerWebExchange exchange = filterAndGetExchange(new StripContextPathGatewayFilterFactory().apply(), | ||
| MockServerWebExchange.from(request)); | ||
|
|
||
| assertThat(exchange.getRequest().getURI()).hasPath("/service/path"); | ||
| assertThat(exchange.getRequest().getPath().contextPath().value()).isEmpty(); | ||
|
|
||
| URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR); | ||
| assertThat(requestUrl).hasScheme("http").hasHost("localhost").hasNoPort().hasPath("/service/path"); | ||
| LinkedHashSet<URI> uris = exchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR); | ||
| assertThat(uris).contains(request.getURI()); | ||
| } | ||
|
|
||
| @Test | ||
| public void stripContextPathFilterToRootWorks() { | ||
| ServerWebExchange exchange = filterAndGetExchange(new StripContextPathGatewayFilterFactory().apply(), | ||
| MockServerWebExchange.from(request("/context", "/context"))); | ||
|
|
||
| assertThat(exchange.getRequest().getURI()).hasPath("/"); | ||
| assertThat(exchange.getRequest().getPath().contextPath().value()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void stripContextPathFilterWithoutContextPathIsNoOp() { | ||
| ServerWebExchange exchange = filterAndGetExchange(new StripContextPathGatewayFilterFactory().apply(), | ||
| MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost/service/path").build())); | ||
|
|
||
| assertThat(exchange.getRequest().getURI()).hasPath("/service/path"); | ||
| assertThat(exchange.getRequest().getPath().contextPath().value()).isEmpty(); | ||
| assertThat(exchange.getAttributes()).doesNotContainKey(GATEWAY_ORIGINAL_REQUEST_URL_ATTR); | ||
| } | ||
|
|
||
| @Test | ||
| public void stripContextPathThenStripPrefixWorks() { | ||
| ServerWebExchange exchange = stripContextPathExchange("/context/service/blue"); | ||
|
|
||
| exchange = filterAndGetExchange(new StripPrefixGatewayFilterFactory().apply(c -> c.setParts(1)), exchange); | ||
|
|
||
| assertThat(exchange.getRequest().getURI()).hasPath("/blue"); | ||
| assertThat(exchange.getRequest().getPath().contextPath().value()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void stripContextPathThenRewritePathWorks() { | ||
| ServerWebExchange exchange = stripContextPathExchange("/context/service/blue"); | ||
|
|
||
| exchange = filterAndGetExchange(new RewritePathGatewayFilterFactory() | ||
| .apply(c -> c.setRegexp("/service/(?<remaining>.*)").setReplacement("/${remaining}")), exchange); | ||
|
|
||
| assertThat(exchange.getRequest().getURI()).hasPath("/blue"); | ||
| assertThat(exchange.getRequest().getPath().contextPath().value()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void stripContextPathThenPrefixPathWorks() { | ||
| ServerWebExchange exchange = stripContextPathExchange("/context/get"); | ||
|
|
||
| exchange = filterAndGetExchange(new PrefixPathGatewayFilterFactory().apply(c -> c.setPrefix("/prefix")), | ||
| exchange); | ||
|
|
||
| assertThat(exchange.getRequest().getURI()).hasPath("/prefix/get"); | ||
| assertThat(exchange.getRequest().getPath().contextPath().value()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void stripContextPathThenSetPathWorks() { | ||
| ServerWebExchange exchange = stripContextPathExchange("/context/service/blue"); | ||
|
|
||
| exchange = filterAndGetExchange(new SetPathGatewayFilterFactory().apply(c -> c.setTemplate("/blue")), exchange); | ||
|
|
||
| assertThat(exchange.getRequest().getURI()).hasPath("/blue"); | ||
| assertThat(exchange.getRequest().getPath().contextPath().value()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void toStringFormat() { | ||
| GatewayFilter filter = new StripContextPathGatewayFilterFactory().apply(); | ||
| assertThat(filter.toString()).contains("StripContextPath"); | ||
| } | ||
|
|
||
| private ServerWebExchange stripContextPathExchange(String path) { | ||
| return filterAndGetExchange(new StripContextPathGatewayFilterFactory().apply(), | ||
| MockServerWebExchange.from(request(path, "/context"))); | ||
| } | ||
|
|
||
| private MockServerHttpRequest request(String path, String contextPath) { | ||
| return MockServerHttpRequest.get("http://localhost" + path).contextPath(contextPath).build(); | ||
| } | ||
|
|
||
| private ServerWebExchange filterAndGetExchange(GatewayFilter filter, ServerWebExchange exchange) { | ||
| GatewayFilterChain filterChain = mock(GatewayFilterChain.class); | ||
| ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class); | ||
| when(filterChain.filter(captor.capture())).thenReturn(Mono.empty()); | ||
|
|
||
| filter.filter(exchange, filterChain); | ||
|
|
||
| return captor.getValue(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big fan of setting things to an empty string. Does
nullwork here instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried
null, but it does not compile here.contextPath(...)is treated as non-null and NullAway fails the build with:passing @Nullable parameter 'null' where @NonNull is requiredSo I kept
contextPath("")for now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, the field in the builder is nullable and in the constructor but not the builder method. @rstoyanchev is that an oversight?