Make it easier to determine each servlet filter's order

Closes gh-17520
This commit is contained in:
Andy Wilkinson 2019-07-15 17:10:32 +01:00
parent 8aed06452d
commit cbc17c237b
2 changed files with 10 additions and 24 deletions

View File

@ -3229,34 +3229,19 @@ If convention-based mapping is not flexible enough, you can use the
`ServletRegistrationBean`, `FilterRegistrationBean`, and `ServletRegistrationBean`, `FilterRegistrationBean`, and
`ServletListenerRegistrationBean` classes for complete control. `ServletListenerRegistrationBean` classes for complete control.
Spring Boot ships with many auto-configurations that may define Filter beans. Here are a Filter beans can be ordered to control their position in the filter chain by using
few examples of Filters and their respective order (lower order value means higher `@Order` or implemented `Ordered`. It is usually safe to leave Filter beans unordered.
precedence): 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
|===
| 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
encoding configuration of your application. If a Servlet filter wraps the request, it 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 should be configured with an order that is less than or equal to
`OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER`. `OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER`.
TIP: To see the order of every `Filter` in your application, enable debug level logging
for the `web` <<boot-features-custom-log-groups,logging group>>
(`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]] [[boot-features-embedded-container-context-initializer]]

View File

@ -260,6 +260,7 @@ public abstract class AbstractFilterRegistrationBean<T extends Filter> extends D
builder.append(" urls=").append(this.urlPatterns); builder.append(" urls=").append(this.urlPatterns);
} }
} }
builder.append(" order=").append(getOrder());
return builder.toString(); return builder.toString();
} }