Browse Source

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.
pull/2805/head
Ignacio Lozano 2 years ago committed by GitHub
parent
commit
04cd541fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java
  2. 18
      spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/cors/CorsGlobalTests.java

1
spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

@ -268,6 +268,7 @@ public class GatewayAutoConfiguration { @@ -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) {

18
spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/cors/CorsGlobalTests.java

@ -19,11 +19,14 @@ package org.springframework.cloud.gateway.cors; @@ -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; @@ -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 { @@ -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();
}
}
}

Loading…
Cancel
Save