Skip to content

Commit 6381a07

Browse files
committed
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
1 parent d74af04 commit 6381a07

File tree

3 files changed

+93
-23
lines changed

3 files changed

+93
-23
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.support;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
22+
/**
23+
* Configuration for {@link ErrorPageFilter}.
24+
*
25+
* @author Andy Wilkinson
26+
*/
27+
@Configuration
28+
class ErrorPageFilterConfiguration {
29+
30+
@Bean
31+
public ErrorPageFilter errorPageFilter() {
32+
return new ErrorPageFilter();
33+
}
34+
35+
}

spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
3232
import org.springframework.boot.web.servlet.ServletContextInitializer;
3333
import org.springframework.context.ApplicationContext;
34-
import org.springframework.context.annotation.Bean;
3534
import org.springframework.context.annotation.Configuration;
3635
import org.springframework.core.annotation.AnnotationUtils;
3736
import org.springframework.util.Assert;
@@ -175,17 +174,4 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
175174
return builder;
176175
}
177176

178-
/**
179-
* Configuration for {@link ErrorPageFilter}.
180-
*/
181-
@Configuration
182-
static class ErrorPageFilterConfiguration {
183-
184-
@Bean
185-
public ErrorPageFilter errorPageFilter() {
186-
return new ErrorPageFilter();
187-
}
188-
189-
}
190-
191177
}

spring-boot/src/test/java/org/springframework/boot/web/support/SpringBootServletInitializerTests.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.web.support;
1818

1919
import javax.servlet.ServletContext;
20+
import javax.servlet.ServletException;
2021

2122
import org.junit.Rule;
2223
import org.junit.Test;
@@ -25,8 +26,14 @@
2526
import org.springframework.beans.DirectFieldAccessor;
2627
import org.springframework.boot.SpringApplication;
2728
import org.springframework.boot.builder.SpringApplicationBuilder;
28-
import org.springframework.boot.web.support.SpringBootServletInitializer.ErrorPageFilterConfiguration;
29+
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
30+
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
31+
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
32+
import org.springframework.boot.web.servlet.ServletContextInitializer;
33+
import org.springframework.context.ConfigurableApplicationContext;
34+
import org.springframework.context.annotation.Bean;
2935
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.context.support.AbstractApplicationContext;
3037
import org.springframework.mock.web.MockServletContext;
3138
import org.springframework.web.context.WebApplicationContext;
3239

@@ -88,11 +95,43 @@ public void mainClassHasSensibleDefault() throws Exception {
8895
}
8996

9097
@Test
91-
public void withErrorPageFilterNotRegistered() throws Exception {
92-
new WithErrorPageFilterNotRegistered()
93-
.createRootApplicationContext(this.servletContext);
94-
assertThat(this.application.getSources())
95-
.containsOnly(WithErrorPageFilterNotRegistered.class);
98+
public void errorPageFilterRegistrationCanBeDisabled() throws Exception {
99+
EmbeddedServletContainer container = new UndertowEmbeddedServletContainerFactory(
100+
0).getEmbeddedServletContainer(new ServletContextInitializer() {
101+
102+
@Override
103+
public void onStartup(ServletContext servletContext)
104+
throws ServletException {
105+
AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilterNotRegistered()
106+
.createRootApplicationContext(servletContext);
107+
try {
108+
assertThat(context.getBeansOfType(ErrorPageFilter.class))
109+
.hasSize(0);
110+
}
111+
finally {
112+
context.close();
113+
}
114+
}
115+
});
116+
try {
117+
container.start();
118+
}
119+
finally {
120+
container.stop();
121+
}
122+
}
123+
124+
@Test
125+
public void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured()
126+
throws Exception {
127+
ConfigurableApplicationContext context = new SpringApplication(
128+
ExecutableWar.class).run();
129+
try {
130+
assertThat(context.getBeansOfType(ErrorPageFilter.class)).hasSize(0);
131+
}
132+
finally {
133+
context.close();
134+
}
96135
}
97136

98137
@Test
@@ -146,15 +185,25 @@ protected SpringApplicationBuilder configure(
146185
}
147186

148187
@Configuration
149-
public class WithErrorPageFilterNotRegistered
150-
extends MockSpringBootServletInitializer {
188+
public static class WithErrorPageFilterNotRegistered
189+
extends SpringBootServletInitializer {
151190

152191
public WithErrorPageFilterNotRegistered() {
153192
setRegisterErrorPageFilter(false);
154193
}
155194

156195
}
157196

197+
@Configuration
198+
public static class ExecutableWar extends SpringBootServletInitializer {
199+
200+
@Bean
201+
public EmbeddedServletContainerFactory containerFactory() {
202+
return new UndertowEmbeddedServletContainerFactory(0);
203+
}
204+
205+
}
206+
158207
@Configuration
159208
public static class Config {
160209

0 commit comments

Comments
 (0)