diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java index 140ac87fd..f1012f9cc 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java @@ -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) { diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/cors/CorsGlobalTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/cors/CorsGlobalTests.java index cb77df12f..d83327234 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/cors/CorsGlobalTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/cors/CorsGlobalTests.java @@ -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(); + } + + } + }