Auto-configure cors on WelcomePageHandlerMapping

Update `WebMvcAutoConfiguration` to automatically apply cors
configuration to the `WelcomePageHandlerMapping`.

Fixes gh-21048
This commit is contained in:
Phillip Webb 2020-04-21 11:38:55 -07:00
parent a2fdf23e41
commit 6011470ba6
2 changed files with 28 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -451,6 +451,7 @@ public class WebMvcAutoConfiguration {
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors());
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
return welcomePageHandlerMapping;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -72,6 +72,7 @@ import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.RequestContextFilter;
@ -83,6 +84,7 @@ import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
@ -554,7 +556,19 @@ public class WebMvcAutoConfigurationTests {
this.contextRunner.withPropertyValues("spring.resources.static-locations:classpath:/welcome-page/")
.run((context) -> {
assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class);
assertThat(context.getBean(WelcomePageHandlerMapping.class).getRootHandler()).isNotNull();
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
assertThat(bean.getRootHandler()).isNotNull();
});
}
@Test
public void welcomePageHandlerIncludesCorsConfiguration() {
this.contextRunner.withPropertyValues("spring.resources.static-locations:classpath:/welcome-page/")
.withUserConfiguration(CorsConfigurer.class).run((context) -> {
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
UrlBasedCorsConfigurationSource source = (UrlBasedCorsConfigurationSource) ReflectionTestUtils
.getField(bean, "corsConfigurationSource");
assertThat(source.getCorsConfigurations()).containsKey("/**");
});
}
@ -1096,4 +1110,14 @@ public class WebMvcAutoConfigurationTests {
}
@Configuration
static class CorsConfigurer implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET");
}
}
}