Skip to content

Commit 09f23a5

Browse files
committed
Update docs on HandlerInterceptor
Closes gh-32729
1 parent 5288504 commit 09f23a5

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/interceptors.adoc

+4-8
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,10 @@ The following example shows how to achieve the same configuration in XML:
5252
</mvc:interceptors>
5353
----
5454

55-
NOTE: Interceptors are not ideally suited as a security layer due to the potential
56-
for a mismatch with annotated controller path matching, which can also match trailing
57-
slashes and path extensions transparently, along with other path matching options. Many
58-
of these options have been deprecated but the potential for a mismatch remains.
59-
Generally, we recommend using Spring Security which includes a dedicated
60-
https://docs.spring.io/spring-security/reference/servlet/integrations/mvc.html#mvc-requestmatcher[MvcRequestMatcher]
61-
to align with Spring MVC path matching and also has a security firewall that blocks many
62-
unwanted characters in URL paths.
55+
WARNING: Interceptors are not ideally suited as a security layer due to the potential for
56+
a mismatch with annotated controller path matching. Generally, we recommend using Spring
57+
Security, or alternatively a similar approach integrated with the Servlet filter chain,
58+
and applied as early as possible.
6359

6460
NOTE: The XML config declares interceptors as `MappedInterceptor` beans, and those are in
6561
turn detected by any `HandlerMapping` bean, including those from other frameworks.
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
[[mvc-handlermapping-interceptor]]
22
= Interception
33

4-
All `HandlerMapping` implementations support handler interceptors that are useful when
5-
you want to apply specific functionality to certain requests -- for example, checking for
6-
a principal. Interceptors must implement `HandlerInterceptor` from the
7-
`org.springframework.web.servlet` package with three methods that should provide enough
8-
flexibility to do all kinds of pre-processing and post-processing:
9-
10-
* `preHandle(..)`: Before the actual handler is run
11-
* `postHandle(..)`: After the handler is run
12-
* `afterCompletion(..)`: After the complete request has finished
13-
14-
The `preHandle(..)` method returns a boolean value. You can use this method to break or
15-
continue the processing of the execution chain. When this method returns `true`, the
16-
handler execution chain continues. When it returns false, the `DispatcherServlet`
17-
assumes the interceptor itself has taken care of requests (and, for example, rendered an
18-
appropriate view) and does not continue executing the other interceptors and the actual
19-
handler in the execution chain.
4+
All `HandlerMapping` implementations support handler interception which is useful when
5+
you want to apply functionality across requests. A `HandlerInterceptor` can implement the
6+
following:
7+
8+
* `preHandle(..)` -- callback before the actual handler is run that returns a boolean.
9+
If the method returns `true`, execution continues; if it returns `false`, the rest of the
10+
execution chain is bypassed and the handler is not called.
11+
* `postHandle(..)` -- callback after the handler is run.
12+
* `afterCompletion(..)` -- callback after the complete request has finished.
13+
14+
NOTE: For `@ResponseBody` and `ResponseEntity` controller methods, the response is written
15+
and committed within the `HandlerAdapter`, before `postHandle` is called. That means it is
16+
too late to change the response, such as to add an extra header. You can implement
17+
`ResponseBodyAdvice` and declare it as an
18+
xref:web/webmvc/mvc-controller/ann-advice.adoc[Controller Advice] bean or configure it
19+
directly on `RequestMappingHandlerAdapter`.
2020

2121
See xref:web/webmvc/mvc-config/interceptors.adoc[Interceptors] in the section on MVC configuration for examples of how to
2222
configure interceptors. You can also register them directly by using setters on individual
2323
`HandlerMapping` implementations.
2424

25-
`postHandle` method is less useful with `@ResponseBody` and `ResponseEntity` methods for
26-
which the response is written and committed within the `HandlerAdapter` and before
27-
`postHandle`. That means it is too late to make any changes to the response, such as adding
28-
an extra header. For such scenarios, you can implement `ResponseBodyAdvice` and either
29-
declare it as an xref:web/webmvc/mvc-controller/ann-advice.adoc[Controller Advice] bean or configure it directly on
30-
`RequestMappingHandlerAdapter`.
31-
32-
33-
25+
WARNING: Interceptors are not ideally suited as a security layer due to the potential for
26+
a mismatch with annotated controller path matching. Generally, we recommend using Spring
27+
Security, or alternatively a similar approach integrated with the Servlet filter chain,
28+
and applied as early as possible.
3429

spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerInterceptor.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -30,9 +30,9 @@
3030
*
3131
* <p>A HandlerInterceptor gets called before the appropriate HandlerAdapter
3232
* triggers the execution of the handler itself. This mechanism can be used
33-
* for a large field of preprocessing aspects, e.g. for authorization checks,
34-
* or common handler behavior like locale or theme changes. Its main purpose
35-
* is to allow for factoring out repetitive handler code.
33+
* for a large field of preprocessing aspects, or common handler behavior
34+
* like locale or theme changes. Its main purpose is to allow for factoring
35+
* out repetitive handler code.
3636
*
3737
* <p>In an asynchronous processing scenario, the handler may be executed in a
3838
* separate thread while the main thread exits without rendering or invoking the
@@ -63,6 +63,12 @@
6363
* forms and GZIP compression. This typically shows when one needs to map the
6464
* filter to certain content types (e.g. images), or to all requests.
6565
*
66+
* <p><strong>Note:</strong> Interceptors are not ideally suited as a security
67+
* layer due to the potential for a mismatch with annotated controller path matching.
68+
* Generally, we recommend using Spring Security, or alternatively a similar
69+
* approach integrated with the Servlet filter chain, and applied as early as
70+
* possible.
71+
*
6672
* @author Juergen Hoeller
6773
* @since 20.06.2003
6874
* @see HandlerExecutionChain#getInterceptors

0 commit comments

Comments
 (0)