From 115ea87b14e77d5251411b1b23cf549de5543956 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 7 Sep 2020 10:41:10 +0100 Subject: [PATCH] Restore ordering of ErrorPageFilter lost in 49f8943 See gh-19471 --- .../web/servlet/support/ErrorPageFilter.java | 9 +++++--- .../support/ErrorPageFilterConfiguration.java | 1 + .../SpringBootServletInitializerTests.java | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java index decd47dc3e..556959ebde 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java @@ -43,7 +43,6 @@ import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPageRegistrar; import org.springframework.boot.web.server.ErrorPageRegistry; import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; import org.springframework.util.ClassUtils; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.NestedServletException; @@ -62,8 +61,7 @@ import org.springframework.web.util.NestedServletException; * @author Andy Wilkinson * @since 2.0.0 */ -@Order(Ordered.HIGHEST_PRECEDENCE + 1) -public class ErrorPageFilter implements Filter, ErrorPageRegistry { +public class ErrorPageFilter implements Filter, ErrorPageRegistry, Ordered { private static final Log logger = LogFactory.getLog(ErrorPageFilter.class); @@ -298,6 +296,11 @@ public class ErrorPageFilter implements Filter, ErrorPageRegistry { public void destroy() { } + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE + 1; + } + private static void addClassIfPresent(Collection> collection, String className) { try { collection.add(ClassUtils.forName(className, null)); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilterConfiguration.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilterConfiguration.java index d019d4c295..68dd15974d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilterConfiguration.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilterConfiguration.java @@ -38,6 +38,7 @@ class ErrorPageFilterConfiguration { @Bean FilterRegistrationBean errorPageFilterRegistration(ErrorPageFilter filter) { FilterRegistrationBean registration = new FilterRegistrationBean<>(filter); + registration.setOrder(filter.getOrder()); registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC); return registration; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java index b1bb09badc..6bada7451d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java @@ -41,6 +41,7 @@ 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.core.Ordered; import org.springframework.core.env.PropertySource; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.WebApplicationContext; @@ -120,6 +121,27 @@ class SpringBootServletInitializerTests { } } + @Test + @SuppressWarnings("rawtypes") + void errorPageFilterIsRegisteredWithNearHighestPrecedence() { + WebServer webServer = new UndertowServletWebServerFactory(0).getWebServer((servletContext) -> { + try (AbstractApplicationContext context = (AbstractApplicationContext) new WithErrorPageFilter() + .createRootApplicationContext(servletContext)) { + Map registrations = context + .getBeansOfType(FilterRegistrationBean.class); + assertThat(registrations).hasSize(1); + FilterRegistrationBean errorPageFilterRegistration = registrations.get("errorPageFilterRegistration"); + assertThat(errorPageFilterRegistration.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE + 1); + } + }); + try { + webServer.start(); + } + finally { + webServer.stop(); + } + } + @Test @SuppressWarnings("rawtypes") void errorPageFilterIsRegisteredForRequestAndAsyncDispatch() {