HandlerInterceptor接口 的讲解(Spring Web MVC的)
Interface HandlerInterceptor
public interface HandlerInterceptor
工作流接口,允许自定义处理程序执行链。应用程序可以为特定处理程序组注册任意数量的现有或自定义拦截器,从而添加常见的预处理行为,而无需修改每个处理程序的实现。
Workflow interface that allows for customized handler execution chains. Applications can register any number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation.
HandlerInterceptor 在相应的 HandlerAdapter 触发处理程序本身的执行之前被调用。此机制可用于大量预处理方面,或常见的处理程序行为,例如语言环境或主题更改。其主要目的是允许分解重复的处理程序代码。
A HandlerInterceptor gets called before the appropriate HandlerAdapter triggers the execution of the handler itself. This mechanism can be used for a large field of preprocessing aspects, or common handler behavior like locale or theme changes. Its main purpose is to allow for factoring out repetitive handler code.
在异步处理场景中,处理程序可以在单独的线程中执行,而主线程退出时不会渲染或调用 postHandle 和 afterCompletion 回调。当并发处理程序执行完成后,请求将被调度回以继续渲染模型,并再次调用此契约的所有方法。有关更多选项和详细信息,请参阅 org.springframework.web.servlet.AsyncHandlerInterceptor。
In an asynchronous processing scenario, the handler may be executed in a separate thread while the main thread exits without rendering or invoking the postHandle and afterCompletion callbacks. When concurrent handler execution completes, the request is dispatched back in order to proceed with rendering the model and all methods of this contract are invoked again. For further options and details see org.springframework.web.servlet.AsyncHandlerInterceptor
通常,每个 HandlerMapping bean 都定义一个拦截器链,并共享其粒度。为了能够将某个拦截器链应用于一组处理程序,需要通过一个 HandlerMapping bean 映射所需的处理程序。拦截器本身在应用程序上下文中定义为 bean,并通过映射 bean 定义中的“interceptors”属性(在 XML 中为 的 )引用。
Typically an interceptor chain is defined per HandlerMapping bean, sharing its granularity. To be able to apply a certain interceptor chain to a group of handlers, one needs to map the desired handlers via one HandlerMapping bean. The interceptors themselves are defined as beans in the application context, referenced by the mapping bean definition via its "interceptors" property (in XML: a of ).
HandlerInterceptor 基本类似于 Servlet 过滤器,但与后者不同的是,它允许自定义预处理(可以选择禁止处理程序本身的执行)和自定义后处理。过滤器功能更强大,例如,它们允许交换在链中传递的请求和响应对象。请注意,过滤器在 web.xml 中配置,而 HandlerInterceptor 则在应用程序上下文中配置。
HandlerInterceptor is basically similar to a Servlet Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context.
作为基本准则,细粒度的处理程序相关的预处理任务是 HandlerInterceptor 实现的候选,尤其是分离出的通用处理程序代码和授权检查。另一方面,过滤器非常适合处理请求内容和视图内容,例如多部分表单和 GZIP 压缩。这通常体现在需要将过滤器映射到特定内容类型(例如,图片)或所有请求时。
As a basic guideline, fine-grained handler-related preprocessing tasks are candidates for HandlerInterceptor implementations, especially factored-out common handler code and authorization checks. On the other hand, a Filter is well-suited for request content and view content handling, like multipart forms and GZIP compression. This typically shows when one needs to map the filter to certain content types (for example, images), or to all requests.
注意:拦截器并不适合用作安全层,因为它可能与带注解的控制器路径匹配不匹配。通常,我们建议使用 Spring Security,或者将类似的方法集成到 Servlet 过滤器链中,并尽早应用。
Note: Interceptors are not ideally suited as a security layer due to the potential for a mismatch with annotated controller path matching. Generally, we recommend using Spring Security, or alternatively a similar approach integrated with the Servlet filter chain, and applied as early as possible.
简介
HandlerInterceptor 是 Spring MVC 框架提供的接口,用于在请求处理的不同阶段进行拦截和处理。它允许开发者在请求到达控制器之前、控制器处理之后以及请求完成后执行自定义逻辑。
🔧 核心方法介绍
HandlerInterceptor 接口定义了三个主要方法
-
preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)- 在请求处理之前执行。
- 返回
true表示继续处理请求,返回false表示中断请求处理。 - 常用于权限验证、日志记录等操作。
-
postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)- 在控制器方法执行之后,视图渲染之前执行。
- 可用于修改
ModelAndView,例如添加公共模型数据。
-
afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)- 在整个请求处理完成后执行,即视图渲染之后。
- 常用于资源清理、异常处理等操作。
📌 应用场景
HandlerInterceptor 常用于以下:
- 权限验证:在请求到达控制器之前验证用户权限。
- 日志记录:记录请求的相关信息,如访问时间、请求参数。
- 性能监控:测量请求处理的时间,进行性能分析。
- 资源管理:在请求完成后释放资源,防止资源泄漏。
如需进一步了解 HandlerInterceptor 的使用和配置,请参考以下源:
- Spring Boot 拦截器的基本使用详解
- HandlerInterceptor三个方法的简单介绍
- [处理器拦截器(HandlerInterceptor)详解](https://www.cnblogs.com/jing99/p/11147152.html

浙公网安备 33010602011771号