Commit b280e309 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent 4b1e5e9c
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -296,7 +296,8 @@ public class WebMvcAutoConfiguration { ...@@ -296,7 +296,8 @@ public class WebMvcAutoConfiguration {
@Bean @Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping( public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) { ResourceProperties resourceProperties) {
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage()); return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
} }
private void customizeResourceHandlerRegistration( private void customizeResourceHandlerRegistration(
...@@ -505,8 +506,9 @@ public class WebMvcAutoConfiguration { ...@@ -505,8 +506,9 @@ public class WebMvcAutoConfiguration {
private static final Log logger = LogFactory private static final Log logger = LogFactory
.getLog(WelcomePageHandlerMapping.class); .getLog(WelcomePageHandlerMapping.class);
private WelcomePageHandlerMapping(Resource welcomePage) { private WelcomePageHandlerMapping(Resource welcomePage,
if (welcomePage != null) { String staticPathPattern) {
if (welcomePage != null && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage); logger.info("Adding welcome page: " + welcomePage);
ParameterizableViewController controller = new ParameterizableViewController(); ParameterizableViewController controller = new ParameterizableViewController();
controller.setViewName("forward:index.html"); controller.setViewName("forward:index.html");
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -564,6 +564,15 @@ public class WebMvcAutoConfigurationTests { ...@@ -564,6 +564,15 @@ public class WebMvcAutoConfigurationTests {
.andExpect(status().isNotFound()); .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 @Test
public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() throws Exception { public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() throws Exception {
load("spring.resources.static-locations:classpath:/welcome-page/"); load("spring.resources.static-locations:classpath:/welcome-page/");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment