Browse Source

Apply global CORS settings to static resources in WebFlux

Closes gh-26495
pull/26504/head
Rossen Stoyanchev 4 years ago
parent
commit
df4ba742cb
  1. 12
      spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java
  2. 13
      spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurer.java
  3. 3
      spring-webflux/src/test/java/org/springframework/web/reactive/config/DelegatingWebFluxConfigurationTests.java
  4. 14
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java

12
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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();

13
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurer.java

@ -23,6 +23,7 @@ import org.springframework.http.codec.ServerCodecConfigurer; @@ -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 { @@ -53,12 +54,16 @@ public interface WebFluxConfigurer {
}
/**
* Configure "global" cross origin request processing.
* <p>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.
* <p>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) {
}

3
spring-webflux/src/test/java/org/springframework/web/reactive/config/DelegatingWebFluxConfigurationTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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));
}

14
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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.
* <p>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) {
}

Loading…
Cancel
Save