Skip to content

Commit ff35c8a

Browse files
committed
Use LinkedHashMap for CORS configurations in CorsGatewayFilterApplicationListener to preserve insertion order. Fixes GH-3805.
Signed-off-by: Yavor Chamov <[email protected]>
1 parent 7b328fa commit ff35c8a

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@ public CorsGatewayFilterApplicationListener(GlobalCorsProperties globalCorsPrope
6464
public void onApplicationEvent(RefreshRoutesResultEvent event) {
6565
routeLocator.getRoutes().collectList().subscribe(routes -> {
6666
// pre-populate with pre-existing global cors configurations to combine with.
67-
var corsConfigurations = new LinkedHashMap<>(globalCorsProperties.getCorsConfigurations());
67+
Map<String, CorsConfiguration> corsConfigurations = new LinkedHashMap<>();
6868

6969
routes.forEach(route -> {
70-
var corsConfiguration = getCorsConfiguration(route);
70+
Optional<CorsConfiguration> corsConfiguration = getCorsConfiguration(route);
7171
corsConfiguration.ifPresent(configuration -> {
7272
var pathPredicate = getPathPredicate(route);
7373
corsConfigurations.put(pathPredicate, configuration);
7474
});
7575
});
7676

77+
corsConfigurations.putAll(globalCorsProperties.getCorsConfigurations());
7778
routePredicateHandlerMapping.setCorsConfigurations(corsConfigurations);
7879
});
7980
}

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/cors/CorsGatewayFilterApplicationListenerTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
/**
4747
* Tests for {@link CorsGatewayFilterApplicationListener}.
4848
*
49-
* <p>This test verifies that the merged CORS configurations composed of global and
50-
* per-route metadata maintain insertion order as defined by the use of {@link LinkedHashMap}.
49+
* <p>This test verifies that the merged CORS configurations - composed of per-route metadata
50+
* and at the global level - maintain insertion order, as defined by the use of {@link LinkedHashMap}.
5151
* Preserving insertion order helps for predictable and deterministic CORS behavior
5252
* when resolving multiple matching path patterns.
5353
* </p>
@@ -56,8 +56,8 @@
5656
* that the resulting configuration map passed to {@link RoutePredicateHandlerMapping#setCorsConfigurations(Map)}
5757
* respects the declared order of:
5858
* <ul>
59-
* <li>Global CORS configurations (in insertion order)</li>
6059
* <li>Route-specific CORS configurations (in the order the routes are discovered)</li>
60+
* <li>Global CORS configurations (in insertion order)</li>
6161
* </ul>
6262
* </p>
6363
*
@@ -118,8 +118,8 @@ void testOnApplicationEvent_preservesInsertionOrder_withRealRoutes() {
118118
verify(handlerMapping).setCorsConfigurations(corsConfigurations.capture());
119119

120120
Map<String, CorsConfiguration> mergedCorsConfigurations = corsConfigurations.getValue();
121-
assertThat(mergedCorsConfigurations.keySet()).containsExactly(GLOBAL_PATH_1,
122-
GLOBAL_PATH_2, ROUTE_PATH_1, ROUTE_PATH_2);
121+
assertThat(mergedCorsConfigurations.keySet())
122+
.containsExactly(ROUTE_PATH_1, ROUTE_PATH_2, GLOBAL_PATH_1, GLOBAL_PATH_2);
123123
assertThat(mergedCorsConfigurations.get(GLOBAL_PATH_1).getAllowedOrigins())
124124
.containsExactly(ORIGIN_GLOBAL_1);
125125
assertThat(mergedCorsConfigurations.get(GLOBAL_PATH_2).getAllowedOrigins())

0 commit comments

Comments
 (0)