Allow Global CORS configuration to be disable (#2779)

Property: spring.cloud.gateway.globalcors.enabled

Reason: CORS filter was applied automatically and it doens't allow developers to customise CORS for specific scenarios. Disabling SCG listener allows developers to customise or reuse CORS logic.
This commit is contained in:
Ignacio Lozano
2022-11-28 17:31:25 +01:00
committed by GitHub
parent 545c684a0f
commit 04cd541fe7
2 changed files with 19 additions and 0 deletions

View File

@@ -268,6 +268,7 @@ public class GatewayAutoConfiguration {
}
@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.globalcors.enabled", matchIfMissing = true)
public CorsGatewayFilterApplicationListener corsGatewayFilterApplicationListener(
GlobalCorsProperties globalCorsProperties, RoutePredicateHandlerMapping routePredicateHandlerMapping,
RouteDefinitionLocator routeDefinitionLocator) {

View File

@@ -19,11 +19,14 @@ package org.springframework.cloud.gateway.cors;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.filter.cors.CorsGatewayFilterApplicationListener;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
@@ -32,6 +35,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
@@ -76,4 +80,18 @@ public class CorsGlobalTests extends BaseWebClientTests {
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfig.class, properties = "spring.cloud.gateway.globalcors.enabled=false")
public static class DisabledByProperty {
@Autowired(required = false)
private CorsGatewayFilterApplicationListener listener;
@org.junit.Test
public void corsGatewayFilterApplicationListenerIsMissing() {
assertThat(listener).isNull();
}
}
}