From 431747928444181f2946966d4a78d32f193f4408 Mon Sep 17 00:00:00 2001
From: Yavor Chamov
Date: Fri, 6 Jun 2025 12:57:11 +0300
Subject: [PATCH 1/6] Use LinkedHashMap for CORS configurations in
CorsGatewayFilterApplicationListener to preserve insertion order. Fixes
GH-3805.
Signed-off-by: Yavor Chamov
---
.../filter/cors/CorsGatewayFilterApplicationListener.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java
index ebe3e148f0..02c4579148 100644
--- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java
+++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java
@@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -64,7 +65,7 @@ public CorsGatewayFilterApplicationListener(GlobalCorsProperties globalCorsPrope
public void onApplicationEvent(RefreshRoutesResultEvent event) {
routeLocator.getRoutes().collectList().subscribe(routes -> {
// pre-populate with pre-existing global cors configurations to combine with.
- var corsConfigurations = new HashMap<>(globalCorsProperties.getCorsConfigurations());
+ var corsConfigurations = new LinkedHashMap<>(globalCorsProperties.getCorsConfigurations());
routes.forEach(route -> {
var corsConfiguration = getCorsConfiguration(route);
From 010a1a1fc7a6980916e61997c49b5bab91013017 Mon Sep 17 00:00:00 2001
From: Yavor Chamov
Date: Fri, 6 Jun 2025 13:22:18 +0300
Subject: [PATCH 2/6] Use LinkedHashMap for CORS configurations in
CorsGatewayFilterApplicationListener to preserve insertion order. Fixes
GH-3805.
Signed-off-by: Yavor Chamov
---
.../filter/cors/CorsGatewayFilterApplicationListener.java | 1 -
1 file changed, 1 deletion(-)
diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java
index 02c4579148..636d09b3ef 100644
--- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java
+++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java
@@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
From efa9e1d801fc33c9efc65d25ae4dd6c58b26fbf0 Mon Sep 17 00:00:00 2001
From: Yavor Chamov
Date: Fri, 13 Jun 2025 16:21:07 +0300
Subject: [PATCH 3/6] Use LinkedHashMap for CORS configurations in
CorsGatewayFilterApplicationListener to preserve insertion order. Fixes
GH-3805.
Signed-off-by: Yavor Chamov
---
...GatewayFilterApplicationListenerTests.java | 155 ++++++++++++++++++
1 file changed, 155 insertions(+)
create mode 100644 spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListenerTests.java
diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListenerTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListenerTests.java
new file mode 100644
index 0000000000..e1dc7b49f2
--- /dev/null
+++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListenerTests.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2013-2020 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.cors;
+
+import java.time.Duration;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import reactor.core.publisher.Flux;
+
+import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
+import org.springframework.cloud.gateway.config.GlobalCorsProperties;
+import org.springframework.cloud.gateway.event.RefreshRoutesResultEvent;
+import org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping;
+import org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory;
+import org.springframework.cloud.gateway.route.Route;
+import org.springframework.cloud.gateway.route.RouteLocator;
+import org.springframework.web.cors.CorsConfiguration;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests for {@link CorsGatewayFilterApplicationListener}.
+ *
+ * This test verifies that the merged CORS configurations composed of global and
+ * per-route metadata maintain insertion order as defined by the use of {@link LinkedHashMap}.
+ * Preserving insertion order helps for predictable and deterministic CORS behavior
+ * when resolving multiple matching path patterns.
+ *
+ *
+ * The test builds actual {@link Route} instances with {@code Path} predicates and verifies
+ * that the resulting configuration map passed to {@link RoutePredicateHandlerMapping#setCorsConfigurations(Map)}
+ * respects the declared order of:
+ *
+ * - Global CORS configurations (in insertion order)
+ * - Route-specific CORS configurations (in the order the routes are discovered)
+ *
+ *
+ *
+ * @author Yavor Chamov
+ */
+@ExtendWith(MockitoExtension.class)
+class CorsGatewayFilterApplicationListenerTests {
+
+ private static final String GLOBAL_PATH_1 = "/global1";
+ private static final String GLOBAL_PATH_2 = "/global2";
+ private static final String ROUTE_PATH_1 = "/route1";
+ private static final String ROUTE_PATH_2 = "/route2";
+ private static final String ORIGIN_GLOBAL_1 = "https://global1.com";
+ private static final String ORIGIN_GLOBAL_2 = "https://global2.com";
+ private static final String ORIGIN_ROUTE_1 = "https://route1.com";
+ private static final String ORIGIN_ROUTE_2 = "https://route2.com";
+ private static final String ROUTE_ID_1 = "route1";
+ private static final String ROUTE_ID_2 = "route2";
+ private static final String ROUTE_URI = "https://spring.io";
+ private static final String METADATA_KEY = "cors";
+ private static final String ALLOWED_ORIGINS_KEY = "allowedOrigins";
+
+ @Mock
+ private RoutePredicateHandlerMapping handlerMapping;
+
+ @Mock
+ private RouteLocator routeLocator;
+
+ @Captor
+ private ArgumentCaptor