springboot(五)——springboot中的拦截器和过滤器小结

栏目: Java · 发布时间: 6年前

内容简介:关于过滤器Filter和拦截器Interceptor,大家都不会陌生,从一开始的servelet,到springmvc,再到现在的springboot,都有接触到,记得刚接触的时候,会容易弄混淆,想写这篇文章做个小的总结第一步:定义Filter第二步:注入springboot容器当中

前言

关于过滤器Filter和拦截器Interceptor,大家都不会陌生,从一开始的servelet,到springmvc,再到现在的springboot,都有接触到,记得刚接触的时候,会容易弄混淆,想写这篇文章做个小的总结

拦截器和过滤器的异同

  • 相同点

    1. 都是aop编程思想的体现,可以在程序执行前后做一些操作,如权限操作,日志记录等
  • 不同点:

    1. Filter是Servlet规范中定义的,拦截器是Spring框架中的
    2. 触发时机不一样,过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的
    3. 拦截器可以获取IOC容器中的各个bean,而过滤器就不行,拦截器归Spring管理

Springboot实现过滤器和拦截器

第一步:定义Filter

@Slf4j
public class TestFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        log.info("TestFilter filter。。。。。。。。");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

第二步:注入springboot容器当中

@Configuration
public class FilterConfig {

    @Bean
    Filter testFilter(){
        return new TestFilter();
    }

    @Bean
    public FilterRegistrationBean<TestFilter> filterRegistrationBean1(){
        FilterRegistrationBean<TestFilter> filterRegistrationBean=new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter((TestFilter) testFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        //filterRegistrationBean.setOrder();多个filter的时候order的数值越小 则优先级越高
        return filterRegistrationBean;
    }
}

第三步:定义拦截器

@Slf4j
@Service(value = "testInterceptor")
public class TestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("TestInterceptor preHandle....");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        log.info("TestInterceptor postHandle....");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        log.info("TestInterceptor afterCompletion....");
    }

}

第四步:加入springboot容器

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    TestInterceptor testInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(testInterceptor)
                .addPathPatterns("/api/**");
    }
}

注意:这边用的springboot是2.0.x,采取的是直接实现WebMvcConfigurer,因为WebMvcConfigurerAdapter被标识了@Deprecated,就没有继承WebMvcConfigurerAdapter了

/** @deprecated */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    public WebMvcConfigurerAdapter() {
    }
}

第五步:还是启动之前的controller

@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {

    @RequestMapping(value = "/hello")
    public String test() {
        log.info("test hello.............");
        return "SUCCESS";
    }

看到打印结果如下

2019-04-27/12:01:04.603||||||||^_^|[http-nio-8088-exec-1] INFO  com.stone.zplxjj.filter.TestFilter 22 - TestFilter filter。。。。。。。。
2019-04-27/12:01:04.612||||||||^_^|[http-nio-8088-exec-1] INFO  com.stone.zplxjj.interceptor.TestInterceptor 26 - TestInterceptor preHandle....
2019-04-27/12:01:04.634||||||||^_^|[http-nio-8088-exec-1] INFO  com.stone.zplxjj.interceptor.TestInterceptor 32 - TestInterceptor postHandle....
2019-04-27/12:01:04.634||||||||^_^|[http-nio-8088-exec-1] INFO  com.stone.zplxjj.interceptor.TestInterceptor 37 - TestInterceptor afterCompletion....

小结

过滤器的实现基于回调函数。而拦截器(代理模式)的实现基于反射,代理又分静态代理和动态代理,动态代理是拦截器的简单实现。那何时使用拦截器?何时使用过滤器?

  • 如果是非spring项目,那么拦截器不能用,只能使用过滤器,这里说的拦截器是基于spring的拦截器。
  • 如果是处理controller前后,既可以使用拦截器也可以使用过滤器,如果都使用了,注意前后顺序。
  • 如果是处理dispaterServlet前后,只能使用过滤器。

更多文章可以关注公众号:stonezplxjj

springboot(五)——springboot中的拦截器和过滤器小结


以上所述就是小编给大家介绍的《springboot(五)——springboot中的拦截器和过滤器小结》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

从点子到产品

从点子到产品

刘飞 / 电子工业出版社 / 2017-1-1 / 49.00元

《从点子到产品:产品经理的价值观与方法论》以产品经理的方法论与价值观为主线,讲述了产品经理在从点子到产品的过程中应该考虑的问题、思考问题的思路,以及如何解决问题的方法。第一部分主要讲述从粗略的点子到具体的方案,要经历的步骤。第二部分主要讲述如何落实方案,以及如何进行用户研究、需求分析和产品设计。第三部分主要讲述在落实方案的过程中要掌握的方法和管理技巧。最后一部分主要讲述产品经理在工作和成长过程中要......一起来看看 《从点子到产品》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换