Skip to content

Commit b280e30

Browse files
committed
Don't forward to welcome page that won't exist due to custom mapping
Previously, WelcomePageHandlerMapping would forward to index.html. This assumed that the static path pattern was always /**. If it had been customised to, for example, /foo/**, then the forward would still be to index.html and a 404 would result as the page is actually available at /foo/index.html. At first glance, it would appear that the forward should be made to foo/index.html. However, as it's a forward rather than a redirect, any relative URLs in the index.html page would then be resolved using / whereas they should be resolved using /foo/. This could be addressed by using a redirect rather than a forward, but we don't want to do that as it's more invasive and would require a roundtrip back to the client. Instead, this commit simply stops performing the forward when the static path pattern is not /**. Closes gh-8788
1 parent 4b1e5e9 commit b280e30

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -296,7 +296,8 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
296296
@Bean
297297
public WelcomePageHandlerMapping welcomePageHandlerMapping(
298298
ResourceProperties resourceProperties) {
299-
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage());
299+
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
300+
this.mvcProperties.getStaticPathPattern());
300301
}
301302

302303
private void customizeResourceHandlerRegistration(
@@ -505,8 +506,9 @@ static final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {
505506
private static final Log logger = LogFactory
506507
.getLog(WelcomePageHandlerMapping.class);
507508

508-
private WelcomePageHandlerMapping(Resource welcomePage) {
509-
if (welcomePage != null) {
509+
private WelcomePageHandlerMapping(Resource welcomePage,
510+
String staticPathPattern) {
511+
if (welcomePage != null && "/**".equals(staticPathPattern)) {
510512
logger.info("Adding welcome page: " + welcomePage);
511513
ParameterizableViewController controller = new ParameterizableViewController();
512514
controller.setViewName("forward:index.html");

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -564,6 +564,15 @@ public void welcomePageMappingProducesNotFoundResponseWhenThereIsNoWelcomePage()
564564
.andExpect(status().isNotFound());
565565
}
566566

567+
@Test
568+
public void welcomePageRootHandlerIsNotRegisteredWhenStaticPathPatternIsNotSlashStarStar() {
569+
load("spring.resources.static-locations:classpath:/welcome-page/",
570+
"spring.mvc.static-path-pattern:/foo/**");
571+
WelcomePageHandlerMapping welcomePageHandlerMapping = this.context
572+
.getBean(WelcomePageHandlerMapping.class);
573+
assertThat(welcomePageHandlerMapping.getRootHandler()).isNull();
574+
}
575+
567576
@Test
568577
public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() throws Exception {
569578
load("spring.resources.static-locations:classpath:/welcome-page/");

0 commit comments

Comments
 (0)