Spring HandlerInterceptor

1.Spring HandlerInterceptor 可以组成一个chain。

这个接口有三个方法:

public interface HandlerInterceptor {

    /**
     * Intercept the execution of a handler. Called after HandlerMapping determined
     * an appropriate handler object, but before HandlerAdapter invokes the handler.
     * <p>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 a HTTP error or writing a custom response.
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler chosen handler to execute, for type and/or instance evaluation
     * @return {@code 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
     */
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception;

    /**
     * Intercept the execution of a handler. Called after HandlerAdapter actually
     * invoked the handler, but before the DispatcherServlet renders the view.
     * Can expose additional model objects to the view via the given ModelAndView.
     * <p>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 post-process an execution,
     * getting applied in inverse order of the execution chain.
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler handler (or {@link HandlerMethod}) that started async
     * execution, for type and/or instance examination
     * @param modelAndView the {@code ModelAndView} that the handler returned
     * (can also be {@code null})
     * @throws Exception in case of errors
     */
    void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception;

    /**
     * Callback after completion of request processing, that is, after rendering
     * the view. Will be called on any outcome of handler execution, thus allows
     * for proper resource cleanup.
     * <p>Note: Will only be called if this interceptor‘s {@code preHandle}
     * method has successfully completed and returned {@code true}!
     * <p>As with the {@code postHandle} method, the method will be invoked on each
     * interceptor in the chain in reverse order, so the first interceptor will be
     * the last to be invoked.
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler handler (or {@link HandlerMethod}) that started async
     * execution, for type and/or instance examination
     * @param ex exception thrown on handler execution, if any
     * @throws Exception in case of errors
     */
    void afterCompletion(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception;

}
preHandle 是在方法执行前被执行,可以用来取消执行postHandle是在方法执行之后执行,返回视图之前执行afterCompletion则是在最后执行,并且它只有在preHandle成功执行,并且返回true才会被执行。               同时此方法执行的顺序:the first interceptor will be the last to be invoked。         先执行的拦截器的这个方法会最后执行。
				
时间: 2024-10-30 19:42:34

Spring HandlerInterceptor的相关文章

Spring HandlerInterceptor的使用

Spring HandlerInterceptor的使用 标签: springexceptionobjectworkflowstringbean 2007-05-25 16:56 21133人阅读 评论(0) 收藏 举报 Spring(19) 版权声明:本文为博主原创文章,未经博主允许不得转载. HandlerInterceptor翻译过来就是spring拦截器,它在某些功能应用上特别有用: 1. 用户是否登陆以及用户权限管理 (见http://www.ideawu.net/ideablog/c

【Spring】7、拦截器HandlerInterceptor

处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器) 类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.   常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面: 3.性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结

[十四]SpringBoot 之 Spring拦截器(HandlerInterceptor)

过滤器属于Servlet范畴的API,与spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但是提供更精细的的控制能力:在request被响应之前.request被响应之后.视图渲染之前以及request全部结束之后.我们不能通过拦截器修改request内容,但是可以通过抛出异常(或者返回false)来暂停request的执

Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例

利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法. 然后在spring-mvc.xml中添加拦截器配置,来指定拦截哪些请求. 步骤一: 创建SomeInterceptor拦截器组件 新建一个com.souvc.interceptor包,在该包中新建一个SomeI

22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

转:http://blog.csdn.net/linxingliang/article/details/52069495 上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但是提供更精细的的控制能力:在request被响应之前.req

(021)Spring Boot之拦截器HandlerInterceptor

 拦截器的使用步骤: 第一步,实现HandlerInterceptor接口,该接口有三个方法preHandle .postHandle .afterCompletion (1)preHandle在controller执行之前调用 (2)postHandle在controller执行之后,页面渲染之前调用 (3)afterCompletion在页面渲染之后调用,一般用于资源清理操作 第二步,继承WebMvcConfigurationSupport或者实现WebMvcConfigurer,重写他们的

spring mvc拦截器HandlerInterceptor

本文主要介绍springmvc中的拦截器,包括拦截器定义和的配置,然后演示了一个链式拦截的测试示例,最后通过一个登录认证的例子展示了拦截器的应用 拦截定义 定义拦截器,实现HandlerInterceptor接口.接口中提供三个方法. public class HandlerInterceptor1 implements HandlerInterceptor{ //进入 Handler方法之前执行 //用于身份认证.身份授权 //比如身份认证,如果认证通过表示当前用户没有登陆,需要此方法拦截不再

spring mvc

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat