Only prefix once

This commit is contained in:
Spencer Gibb
2018-04-02 12:58:34 -04:00
parent ae0e7cc76e
commit 61badf8efe
2 changed files with 10 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ALREADY_PREFIXED_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
@@ -49,6 +50,13 @@ public class PrefixPathGatewayFilterFactory extends AbstractGatewayFilterFactory
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
boolean alreadyPrefixed = exchange.getAttributeOrDefault(GATEWAY_ALREADY_PREFIXED_ATTR, false);
if (alreadyPrefixed) {
return chain.filter(exchange);
}
exchange.getAttributes().put(GATEWAY_ALREADY_PREFIXED_ATTR, true);
ServerHttpRequest req = exchange.getRequest();
addOriginalRequestUrl(exchange, req.getURI());
String newPath = config.prefix + req.getURI().getPath();

View File

@@ -50,6 +50,8 @@ public class ServerWebExchangeUtils {
*/
public static final String GATEWAY_ALREADY_ROUTED_ATTR = qualify("gatewayAlreadyRouted");
public static final String GATEWAY_ALREADY_PREFIXED_ATTR = qualify("gatewayAlreadyPrefixed");
private static String qualify(String attr) {
return ServerWebExchangeUtils.class.getName() + "." + attr;
}