diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java index 16661f20db..fd669855a2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java @@ -28,6 +28,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; import org.springframework.core.log.LogMessage; import org.springframework.http.HttpHeaders; +import org.springframework.http.InvalidMediaTypeException; import org.springframework.http.MediaType; import org.springframework.util.StringUtils; import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; @@ -40,6 +41,7 @@ import org.springframework.web.servlet.mvc.ParameterizableViewController; * * @author Andy Wilkinson * @author Bruce Brouwer + * @author Moritz Halbritter * @see WelcomePageNotAcceptableHandlerMapping */ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping { @@ -79,7 +81,13 @@ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping { private List getAcceptedMediaTypes(HttpServletRequest request) { String acceptHeader = request.getHeader(HttpHeaders.ACCEPT); if (StringUtils.hasText(acceptHeader)) { - return MediaType.parseMediaTypes(acceptHeader); + try { + return MediaType.parseMediaTypes(acceptHeader); + } + catch (InvalidMediaTypeException ex) { + logger.warn("Received invalid Accept header. Assuming all media types are accepted", + logger.isDebugEnabled() ? ex : null); + } } return MEDIA_TYPES_ALL; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java index b706b5e68b..96462cf037 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java @@ -22,6 +22,7 @@ import java.util.Map; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Value; @@ -30,6 +31,8 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -53,7 +56,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * Tests for {@link WelcomePageHandlerMapping}. * * @author Andy Wilkinson + * @author Moritz Halbritter */ +@ExtendWith(OutputCaptureExtension.class) class WelcomePageHandlerMappingTests { private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() @@ -163,6 +168,17 @@ class WelcomePageHandlerMappingTests { }); } + @Test + void logsInvalidAcceptHeader(CapturedOutput output) { + this.contextRunner.withUserConfiguration(TemplateConfiguration.class).run((context) -> { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); + mockMvc.perform(get("/").accept("*/*q=0.8")) + .andExpect(status().isOk()) + .andExpect(content().string("index template")); + }); + assertThat(output).contains("Received invalid Accept header. Assuming all media types are accepted"); + } + @Configuration(proxyBeanMethods = false) static class HandlerMappingConfiguration {