Skip to content

Allow stripPrefix to be less strict about contextPaths #3009

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ _site/
*.swo
.vscode/
.flattened-pom.xml
.devcontainer/
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ public Mono<Void> 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());
Expand All @@ -89,7 +92,7 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
@Override
public String toString() {
return filterToStringCreator(StripPrefixGatewayFilterFactory.this).append("parts", config.getParts())
.toString();
.append("enforeContextPath", config.getEnforceContextPath()).toString();
}
};
}
Expand All @@ -98,6 +101,8 @@ public static class Config {

private int parts = 1;

private boolean enforceContextPath = true;

public int getParts() {
return parts;
}
Expand All @@ -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;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down