Skip to content

Commit 49f8943

Browse files
committed
Register ErrorPageFilter for async dispatch
Fixes gh-19471
1 parent 22dc4e7 commit 49f8943

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilterConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.web.servlet.support;
1818

19+
import javax.servlet.DispatcherType;
20+
21+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
1922
import org.springframework.context.annotation.Bean;
2023
import org.springframework.context.annotation.Configuration;
2124

@@ -32,4 +35,11 @@ ErrorPageFilter errorPageFilter() {
3235
return new ErrorPageFilter();
3336
}
3437

38+
@Bean
39+
FilterRegistrationBean<ErrorPageFilter> errorPageFilterRegistration(ErrorPageFilter filter) {
40+
FilterRegistrationBean<ErrorPageFilter> registration = new FilterRegistrationBean<>(filter);
41+
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
42+
return registration;
43+
}
44+
3545
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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,7 +17,10 @@
1717
package org.springframework.boot.web.servlet.support;
1818

1919
import java.util.Collections;
20+
import java.util.EnumSet;
21+
import java.util.Map;
2022

23+
import javax.servlet.DispatcherType;
2124
import javax.servlet.ServletContext;
2225

2326
import org.junit.jupiter.api.AfterEach;
@@ -31,6 +34,7 @@
3134
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
3235
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
3336
import org.springframework.boot.web.server.WebServer;
37+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
3438
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
3539
import org.springframework.context.ApplicationListener;
3640
import org.springframework.context.ConfigurableApplicationContext;
@@ -116,6 +120,28 @@ void errorPageFilterRegistrationCanBeDisabled() {
116120
}
117121
}
118122

123+
@Test
124+
@SuppressWarnings("rawtypes")
125+
void errorPageFilterIsRegisteredForRequestAndAsyncDispatch() {
126+
WebServer webServer = new UndertowServletWebServerFactory(0).getWebServer((servletContext) -> {
127+
try (AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilter()
128+
.createRootApplicationContext(servletContext)) {
129+
Map<String, FilterRegistrationBean> registrations = context
130+
.getBeansOfType(FilterRegistrationBean.class);
131+
assertThat(registrations).hasSize(1);
132+
FilterRegistrationBean errorPageFilterRegistration = registrations.get("errorPageFilterRegistration");
133+
assertThat(errorPageFilterRegistration).hasFieldOrPropertyWithValue("dispatcherTypes",
134+
EnumSet.of(DispatcherType.ASYNC, DispatcherType.REQUEST));
135+
}
136+
});
137+
try {
138+
webServer.start();
139+
}
140+
finally {
141+
webServer.stop();
142+
}
143+
}
144+
119145
@Test
120146
void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured() {
121147
try (ConfigurableApplicationContext context = new SpringApplication(ExecutableWar.class).run()) {
@@ -199,6 +225,11 @@ static class WithErrorPageFilterNotRegistered extends SpringBootServletInitializ
199225

200226
}
201227

228+
@Configuration(proxyBeanMethods = false)
229+
static class WithErrorPageFilter extends SpringBootServletInitializer {
230+
231+
}
232+
202233
@Configuration(proxyBeanMethods = false)
203234
static class ExecutableWar extends SpringBootServletInitializer {
204235

0 commit comments

Comments
 (0)