From 6381a07c71310c56dc29cf99709adf5fe6e6406a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 3 Mar 2017 11:38:13 +0000 Subject: [PATCH] Prevent ErrorPageFilter from being used if SBServletInitializer is used Previously, the configuration class that produces the ErrorPageFilter bean was an inner class of SpringBootServletInitializer. As a result, whenever SpringBootServletInitializer was subclasses, the ErrorPageFilter would be used. This adversely affected applications running as an executable war file and those deployed to a container with error page filter registration disabled. This commit makes ErrorPageFilterConfiguration a top-level class so that it is no longer always found via SpringBootServletInitializer. The test that verifies that error page filter registration can be disabled has been updated to more accurately simulate the behaviour when an application is deployed as a war to a standalone container. A test that mimics the behaviour of an application run as an executable war has also been added. Closes gh-8477 --- .../support/ErrorPageFilterConfiguration.java | 35 ++++++++++ .../support/SpringBootServletInitializer.java | 14 ---- .../SpringBootServletInitializerTests.java | 67 ++++++++++++++++--- 3 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilterConfiguration.java diff --git a/spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilterConfiguration.java b/spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilterConfiguration.java new file mode 100644 index 0000000000..c2e078be2e --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/web/support/ErrorPageFilterConfiguration.java @@ -0,0 +1,35 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.web.support; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Configuration for {@link ErrorPageFilter}. + * + * @author Andy Wilkinson + */ +@Configuration +class ErrorPageFilterConfiguration { + + @Bean + public ErrorPageFilter errorPageFilter() { + return new ErrorPageFilter(); + } + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java b/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java index ceca449aa7..717cd102c1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java @@ -31,7 +31,6 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.Assert; @@ -175,17 +174,4 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit return builder; } - /** - * Configuration for {@link ErrorPageFilter}. - */ - @Configuration - static class ErrorPageFilterConfiguration { - - @Bean - public ErrorPageFilter errorPageFilter() { - return new ErrorPageFilter(); - } - - } - } diff --git a/spring-boot/src/test/java/org/springframework/boot/web/support/SpringBootServletInitializerTests.java b/spring-boot/src/test/java/org/springframework/boot/web/support/SpringBootServletInitializerTests.java index 382f4637d9..684ec46c53 100644 --- a/spring-boot/src/test/java/org/springframework/boot/web/support/SpringBootServletInitializerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/support/SpringBootServletInitializerTests.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. @@ -17,6 +17,7 @@ package org.springframework.boot.web.support; import javax.servlet.ServletContext; +import javax.servlet.ServletException; import org.junit.Rule; import org.junit.Test; @@ -25,8 +26,14 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.DirectFieldAccessor; import org.springframework.boot.SpringApplication; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.support.SpringBootServletInitializer.ErrorPageFilterConfiguration; +import org.springframework.boot.context.embedded.EmbeddedServletContainer; +import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; +import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory; +import org.springframework.boot.web.servlet.ServletContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.AbstractApplicationContext; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.WebApplicationContext; @@ -88,11 +95,43 @@ public class SpringBootServletInitializerTests { } @Test - public void withErrorPageFilterNotRegistered() throws Exception { - new WithErrorPageFilterNotRegistered() - .createRootApplicationContext(this.servletContext); - assertThat(this.application.getSources()) - .containsOnly(WithErrorPageFilterNotRegistered.class); + public void errorPageFilterRegistrationCanBeDisabled() throws Exception { + EmbeddedServletContainer container = new UndertowEmbeddedServletContainerFactory( + 0).getEmbeddedServletContainer(new ServletContextInitializer() { + + @Override + public void onStartup(ServletContext servletContext) + throws ServletException { + AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilterNotRegistered() + .createRootApplicationContext(servletContext); + try { + assertThat(context.getBeansOfType(ErrorPageFilter.class)) + .hasSize(0); + } + finally { + context.close(); + } + } + }); + try { + container.start(); + } + finally { + container.stop(); + } + } + + @Test + public void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured() + throws Exception { + ConfigurableApplicationContext context = new SpringApplication( + ExecutableWar.class).run(); + try { + assertThat(context.getBeansOfType(ErrorPageFilter.class)).hasSize(0); + } + finally { + context.close(); + } } @Test @@ -146,8 +185,8 @@ public class SpringBootServletInitializerTests { } @Configuration - public class WithErrorPageFilterNotRegistered - extends MockSpringBootServletInitializer { + public static class WithErrorPageFilterNotRegistered + extends SpringBootServletInitializer { public WithErrorPageFilterNotRegistered() { setRegisterErrorPageFilter(false); @@ -155,6 +194,16 @@ public class SpringBootServletInitializerTests { } + @Configuration + public static class ExecutableWar extends SpringBootServletInitializer { + + @Bean + public EmbeddedServletContainerFactory containerFactory() { + return new UndertowEmbeddedServletContainerFactory(0); + } + + } + @Configuration public static class Config {