HandlerInterceptor接口的 方法preHandle

文档地址 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html

preHandle

default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception

处理程序执行前的拦截点。在 HandlerMapping 确定合适的处理程序对象之后,但在 HandlerAdapter 调用该处理程序之前调用。
Interception point before the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.

DispatcherServlet 处理执行链中的处理程序,该执行链由任意数量的拦截器组成,处理程序本身位于执行链的末尾。使用此方法,每个拦截器都可以决定中止执行链,通常是发送 HTTP 错误或编写自定义响应。
DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending an HTTP error or writing a custom response.

注意:异步请求处理有特殊注意事项。更多详情请参阅 AsyncHandlerInterceptor。
Note: special considerations apply for asynchronous request processing. For more details see AsyncHandlerInterceptor.

默认实现返回 true
The default implementation returns true.

Parameters:

  • request - current HTTP request
  • response - current HTTP response
  • handler - chosen handler to execute, for type and/or instance evaluation

Returns:
如果执行链应该继续执行下一个拦截器或处理程序本身,则返回 true。否则,DispatcherServlet 会假定此拦截器已经处理了响应本身。
true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

Throws:
Exception - in case of errors

posted @ 2025-04-12 15:54  kuki'  阅读(54)  评论(0)    收藏  举报