From cbc17c237beec1172833f4fedba40accea18d5f0 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 15 Jul 2019 17:10:32 +0100 Subject: [PATCH] Make it easier to determine each servlet filter's order Closes gh-17520 --- .../main/asciidoc/spring-boot-features.adoc | 33 +++++-------------- .../AbstractFilterRegistrationBean.java | 1 + 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index b93fab96fc5..5cec696d254 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -3229,34 +3229,19 @@ If convention-based mapping is not flexible enough, you can use the `ServletRegistrationBean`, `FilterRegistrationBean`, and `ServletListenerRegistrationBean` classes for complete control. -Spring Boot ships with many auto-configurations that may define Filter beans. Here are a -few examples of Filters and their respective order (lower order value means higher -precedence): - -|=== -| Servlet Filter | Order - -|`OrderedCharacterEncodingFilter` -|`Ordered.HIGHEST_PRECEDENCE` - -|`WebMvcMetricsFilter` -|`Ordered.HIGHEST_PRECEDENCE + 1` - -|`ErrorPageFilter` -|`Ordered.HIGHEST_PRECEDENCE + 1` - -|`HttpTraceFilter` -|`Ordered.LOWEST_PRECEDENCE - 10` -|=== - -It is usually safe to leave Filter beans unordered. - -If a specific order is required, you should avoid configuring a Filter that reads the -request body at `Ordered.HIGHEST_PRECEDENCE`, since it might go against the character +Filter beans can be ordered to control their position in the filter chain by using +`@Order` or implemented `Ordered`. It is usually safe to leave Filter beans unordered. +However, if a specific order is required, you should avoid configuring a Filter that reads +the request body at `Ordered.HIGHEST_PRECEDENCE`, since it might go against the character encoding configuration of your application. If a Servlet filter wraps the request, it should be configured with an order that is less than or equal to `OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER`. +TIP: To see the order of every `Filter` in your application, enable debug level logging +for the `web` <> +(`logging.level.web=debug`). Details of the registered filters, including their order and +URL patterns, will then be logged at startup. + [[boot-features-embedded-container-context-initializer]] diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java index f51dbaedf9f..4c0593d93f0 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java @@ -260,6 +260,7 @@ public abstract class AbstractFilterRegistrationBean extends D builder.append(" urls=").append(this.urlPatterns); } } + builder.append(" order=").append(getOrder()); return builder.toString(); }