From b280e3092daaefff9c711611758d75468e4401ef Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 4 Apr 2017 10:13:24 +0100 Subject: [PATCH] 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 --- .../autoconfigure/web/WebMvcAutoConfiguration.java | 10 ++++++---- .../web/WebMvcAutoConfigurationTests.java | 11 ++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index fe03fd1761..c8156c4bcd 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -296,7 +296,8 @@ public class WebMvcAutoConfiguration { @Bean public WelcomePageHandlerMapping welcomePageHandlerMapping( ResourceProperties resourceProperties) { - return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage()); + return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(), + this.mvcProperties.getStaticPathPattern()); } private void customizeResourceHandlerRegistration( @@ -505,8 +506,9 @@ public class WebMvcAutoConfiguration { private static final Log logger = LogFactory .getLog(WelcomePageHandlerMapping.class); - private WelcomePageHandlerMapping(Resource welcomePage) { - if (welcomePage != null) { + private WelcomePageHandlerMapping(Resource welcomePage, + String staticPathPattern) { + if (welcomePage != null && "/**".equals(staticPathPattern)) { logger.info("Adding welcome page: " + welcomePage); ParameterizableViewController controller = new ParameterizableViewController(); controller.setViewName("forward:index.html"); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index 25f9c18bbb..fe00c76472 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -564,6 +564,15 @@ public class WebMvcAutoConfigurationTests { .andExpect(status().isNotFound()); } + @Test + public void welcomePageRootHandlerIsNotRegisteredWhenStaticPathPatternIsNotSlashStarStar() { + load("spring.resources.static-locations:classpath:/welcome-page/", + "spring.mvc.static-path-pattern:/foo/**"); + WelcomePageHandlerMapping welcomePageHandlerMapping = this.context + .getBean(WelcomePageHandlerMapping.class); + assertThat(welcomePageHandlerMapping.getRootHandler()).isNull(); + } + @Test public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() throws Exception { load("spring.resources.static-locations:classpath:/welcome-page/");