Skip to content

Commit 7d0b14f

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-21070
2 parents 8cbd7f5 + 6011470 commit 7d0b14f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext ap
402402
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
403403
this.mvcProperties.getStaticPathPattern());
404404
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
405+
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
405406
return welcomePageHandlerMapping;
406407
}
407408

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
7777
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
7878
import org.springframework.web.context.request.ServletWebRequest;
79+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
7980
import org.springframework.web.filter.FormContentFilter;
8081
import org.springframework.web.filter.HiddenHttpMethodFilter;
8182
import org.springframework.web.filter.RequestContextFilter;
@@ -87,6 +88,7 @@
8788
import org.springframework.web.servlet.ViewResolver;
8889
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
8990
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
91+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
9092
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
9193
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9294
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
@@ -536,7 +538,19 @@ void welcomePageHandlerMappingIsAutoConfigured() {
536538
this.contextRunner.withPropertyValues("spring.resources.static-locations:classpath:/welcome-page/")
537539
.run((context) -> {
538540
assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class);
539-
assertThat(context.getBean(WelcomePageHandlerMapping.class).getRootHandler()).isNotNull();
541+
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
542+
assertThat(bean.getRootHandler()).isNotNull();
543+
});
544+
}
545+
546+
@Test
547+
void welcomePageHandlerIncludesCorsConfiguration() {
548+
this.contextRunner.withPropertyValues("spring.resources.static-locations:classpath:/welcome-page/")
549+
.withUserConfiguration(CorsConfigurer.class).run((context) -> {
550+
WelcomePageHandlerMapping bean = context.getBean(WelcomePageHandlerMapping.class);
551+
UrlBasedCorsConfigurationSource source = (UrlBasedCorsConfigurationSource) ReflectionTestUtils
552+
.getField(bean, "corsConfigurationSource");
553+
assertThat(source.getCorsConfigurations()).containsKey("/**");
540554
});
541555
}
542556

@@ -1157,4 +1171,14 @@ public Example parse(String source, Locale locale) {
11571171

11581172
}
11591173

1174+
@Configuration
1175+
static class CorsConfigurer implements WebMvcConfigurer {
1176+
1177+
@Override
1178+
public void addCorsMappings(CorsRegistry registry) {
1179+
registry.addMapping("/**").allowedMethods("GET");
1180+
}
1181+
1182+
}
1183+
11601184
}

0 commit comments

Comments
 (0)