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
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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/");
|
||||
|
||||
Reference in New Issue
Block a user