From df4ba742cbc7f64a58390624d806e935b3c092af Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 3 Feb 2021 11:17:59 +0000 Subject: [PATCH] Apply global CORS settings to static resources in WebFlux Closes gh-26495 --- .../config/WebFluxConfigurationSupport.java | 12 ++---------- .../web/reactive/config/WebFluxConfigurer.java | 13 +++++++++---- .../DelegatingWebFluxConfigurationTests.java | 3 ++- .../config/annotation/WebMvcConfigurer.java | 14 ++++++++++++-- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java index 2338540103..828ac8aa08 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -245,15 +245,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware { AbstractHandlerMapping handlerMapping = registry.getHandlerMapping(); if (handlerMapping != null) { - PathMatchConfigurer configurer = getPathMatchConfigurer(); - Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch(); - Boolean useCaseSensitiveMatch = configurer.isUseCaseSensitiveMatch(); - if (useTrailingSlashMatch != null) { - handlerMapping.setUseTrailingSlashMatch(useTrailingSlashMatch); - } - if (useCaseSensitiveMatch != null) { - handlerMapping.setUseCaseSensitiveMatch(useCaseSensitiveMatch); - } + configureAbstractHandlerMapping(handlerMapping, getPathMatchConfigurer()); } else { handlerMapping = new EmptyHandlerMapping(); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurer.java index 43280489f7..6bfabed26a 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurer.java @@ -23,6 +23,7 @@ import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.lang.Nullable; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; +import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder; import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer; import org.springframework.web.reactive.socket.server.WebSocketService; @@ -53,12 +54,16 @@ public interface WebFluxConfigurer { } /** - * Configure "global" cross origin request processing. - *

The configured readers and writers will apply to all requests including - * annotated controllers and functional endpoints. Annotated controllers can - * further declare more fine-grained configuration via + * Configure "global" cross origin request processing. The configured CORS + * mappings apply to annotated controllers, functional endpoints, and static + * resources. + *

Annotated controllers can further declare more fine-grained config via * {@link org.springframework.web.bind.annotation.CrossOrigin @CrossOrigin}. + * In such cases "global" CORS configuration declared here is + * {@link org.springframework.web.cors.CorsConfiguration#combine(CorsConfiguration) combined} + * with local CORS configuration defined on a controller method. * @see CorsRegistry + * @see CorsConfiguration#combine(CorsConfiguration) */ default void addCorsMappings(CorsRegistry registry) { } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/config/DelegatingWebFluxConfigurationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/config/DelegatingWebFluxConfigurationTests.java index 1d592013ed..6caa8c26f4 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/config/DelegatingWebFluxConfigurationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/config/DelegatingWebFluxConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -126,6 +126,7 @@ public class DelegatingWebFluxConfigurationTests { delegatingConfig.resourceHandlerMapping(delegatingConfig.resourceUrlProvider()); verify(webFluxConfigurer).addResourceHandlers(any(ResourceHandlerRegistry.class)); + verify(webFluxConfigurer).addCorsMappings(any(CorsRegistry.class)); verify(webFluxConfigurer).configurePathMatching(any(PathMatchConfigurer.class)); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java index df771df2d2..25524520b3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -25,6 +25,7 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.lang.Nullable; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; +import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.DispatcherServlet; @@ -104,8 +105,17 @@ public interface WebMvcConfigurer { } /** - * Configure cross origin requests processing. + * Configure "global" cross origin request processing. The configured CORS + * mappings apply to annotated controllers, functional endpoints, and static + * resources. + *

Annotated controllers can further declare more fine-grained config via + * {@link org.springframework.web.bind.annotation.CrossOrigin @CrossOrigin}. + * In such cases "global" CORS configuration declared here is + * {@link org.springframework.web.cors.CorsConfiguration#combine(CorsConfiguration) combined} + * with local CORS configuration defined on a controller method. * @since 4.2 + * @see CorsRegistry + * @see CorsConfiguration#combine(CorsConfiguration) */ default void addCorsMappings(CorsRegistry registry) { }