diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java index 4594ec769..2f684ddc3 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java @@ -18,6 +18,7 @@ package org.springframework.cloud.gateway.filter.factory; import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; import reactor.core.publisher.Mono; @@ -59,13 +60,14 @@ public class RewritePathGatewayFilterFactory @Override public GatewayFilter apply(Config config) { String replacement = config.replacement.replace("$\\", "$"); + Pattern pattern = Pattern.compile(config.regexp); return new GatewayFilter() { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest req = exchange.getRequest(); addOriginalRequestUrl(exchange, req.getURI()); String path = req.getURI().getRawPath(); - String newPath = path.replaceAll(config.regexp, replacement); + String newPath = pattern.matcher(path).replaceAll(replacement); ServerHttpRequest request = req.mutate().path(newPath).build(); diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRoutePredicateFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRoutePredicateFactory.java index 8501191f7..d5382866a 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRoutePredicateFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRoutePredicateFactory.java @@ -20,10 +20,11 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.function.Predicate; +import java.util.regex.Pattern; import jakarta.validation.constraints.NotEmpty; -import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.server.ServerWebExchange; @@ -53,7 +54,7 @@ public class HeaderRoutePredicateFactory extends AbstractRoutePredicateFactory apply(Config config) { - boolean hasRegex = !ObjectUtils.isEmpty(config.regexp); + Pattern pattern = (StringUtils.hasText(config.regexp)) ? Pattern.compile(config.regexp) : null; return new GatewayPredicate() { @Override @@ -64,11 +65,11 @@ public class HeaderRoutePredicateFactory extends AbstractRoutePredicateFactory