Skip to content

Commit 6011470

Browse files
committed
Auto-configure cors on WelcomePageHandlerMapping
Update `WebMvcAutoConfiguration` to automatically apply cors configuration to the `WelcomePageHandlerMapping`. Fixes gh-21048
1 parent a2fdf23 commit 6011470

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -451,6 +451,7 @@ public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext ap
451451
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
452452
this.mvcProperties.getStaticPathPattern());
453453
welcomePageHandlerMapping.setInterceptors(getInterceptors());
454+
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
454455
return welcomePageHandlerMapping;
455456
}
456457

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,6 +72,7 @@
7272
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
7373
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
7474
import org.springframework.web.context.request.ServletWebRequest;
75+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
7576
import org.springframework.web.filter.FormContentFilter;
7677
import org.springframework.web.filter.HiddenHttpMethodFilter;
7778
import org.springframework.web.filter.RequestContextFilter;
@@ -83,6 +84,7 @@
8384
import org.springframework.web.servlet.ViewResolver;
8485
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
8586
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
87+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
8688
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
8789
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8890
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
@@ -554,7 +556,19 @@ public void welcomePageHandlerMappingIsAutoConfigured() {
554556
this.contextRunner.withPropertyValues("spring.resources.static-locations:classpath:/welcome-page/")
555557
.run((context) -> {
556558
assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class);
557-
assertThat(context.getBean(WelcomePageHandlerMapping.class).getRootHandler()).isNotNull();
559+
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
560+
assertThat(bean.getRootHandler()).isNotNull();
561+
});
562+
}
563+
564+
@Test
565+
public void welcomePageHandlerIncludesCorsConfiguration() {
566+
this.contextRunner.withPropertyValues("spring.resources.static-locations:classpath:/welcome-page/")
567+
.withUserConfiguration(CorsConfigurer.class).run((context) -> {
568+
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
569+
UrlBasedCorsConfigurationSource source = (UrlBasedCorsConfigurationSource) ReflectionTestUtils
570+
.getField(bean, "corsConfigurationSource");
571+
assertThat(source.getCorsConfigurations()).containsKey("/**");
558572
});
559573
}
560574

@@ -1096,4 +1110,14 @@ public FilterRegistrationBean<RequestContextFilter> customRequestContextFilterRe
10961110

10971111
}
10981112

1113+
@Configuration
1114+
static class CorsConfigurer implements WebMvcConfigurer {
1115+
1116+
@Override
1117+
public void addCorsMappings(CorsRegistry registry) {
1118+
registry.addMapping("/**").allowedMethods("GET");
1119+
}
1120+
1121+
}
1122+
10991123
}

0 commit comments

Comments
 (0)