SpringBoot之拦截器(E)

来源:互联网 发布:淘宝宝贝访客突然增加 编辑:程序博客网 时间:2024/06/05 03:56

Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器)。
HandlerInterceptor 的功能跟过滤器类似,但是提供更精细的的控制能力:在request被响应之前、request被响应之后、视图渲染之前以及request全部结束之后。我们不能通过拦截器修改request内容,但是可以通过抛出异常(或者返回false)来暂停request的执行。
实现自定义拦截器只需要3步:
1、创建我们自己的拦截器类并实现 HandlerInterceptor 接口。
2、创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。
2、实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)。
PS:本文重点是如何在Spring-Boot中使用拦截器

MyInterceptor1.java

package cn.wuyang.springboot.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;/** * 自定义拦截器1 *  1.实现HandlerInterceptor 接口 */public class MyInterceptor1 implements HandlerInterceptor  {    @Override    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)            throws Exception {        System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)");    }    @Override    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)            throws Exception {        System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");    }    @Override    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {        System.out.println(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用(Controller方法调用之前)");        return true;// 只有返回true才会继续向下执行,返回false取消当前请求    }}

MyInterceptor2.java

package cn.wuyang.springboot.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;/** * 自定义拦截器2 *  1.实现HandlerInterceptor 接口 */public class MyInterceptor2 implements HandlerInterceptor  {    @Override    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)            throws Exception {        System.out.println(">>>MyInterceptor2>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)");    }    @Override    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)            throws Exception {        System.out.println(">>>MyInterceptor2>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");    }    @Override    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {        System.out.println(">>>MyInterceptor2>>>>>>>在请求处理之前进行调用(Controller方法调用之前)");        return true;// 只有返回true才会继续向下执行,返回false取消当前请求    }}

MyWebAppConfigurer.java

package cn.wuyang.springboot.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import cn.wuyang.springboot.interceptor.MyInterceptor1;import cn.wuyang.springboot.interceptor.MyInterceptor2;/** * 继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。 * @author wuyang * */@Configurationpublic class MyWebAppConfigurer extends WebMvcConfigurerAdapter {    @Override    public void addInterceptors(InterceptorRegistry registry) {        // 多个拦截器组成一个拦截器链        // addPathPatterns 用于添加拦截规则        // excludePathPatterns 用户排除拦截        registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**");        registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");        super.addInterceptors(registry);    }}

然后在浏览器输入地址: http://127.0.0.1:8080/hello 后,控制台的输出为:

>>>MyInterceptor1>>>>>>>在请求处理之前进行调用(Controller方法调用之前)>>>MyInterceptor2>>>>>>>在请求处理之前进行调用(Controller方法调用之前)>>>MyInterceptor2>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)>>>MyInterceptor1>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)>>>MyInterceptor2>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)>>>MyInterceptor1>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)

http://127.0.0.1:8080/myservlet 是不会被拦截器拦截的。并且不管是属于哪个Servlet 只要复合过滤器的过滤规则,过滤器都会拦截。控制台的输出为:

>>>>>>>>>>doGet()<<<<<<<<<<<>>>>>>>>>>doPost()<<<<<<<<<<<