内容简介:关于过滤器Filter和拦截器Interceptor,大家都不会陌生,从一开始的servelet,到springmvc,再到现在的springboot,都有接触到,记得刚接触的时候,会容易弄混淆,想写这篇文章做个小的总结第一步:定义Filter第二步:注入springboot容器当中
前言
关于过滤器Filter和拦截器Interceptor,大家都不会陌生,从一开始的servelet,到springmvc,再到现在的springboot,都有接触到,记得刚接触的时候,会容易弄混淆,想写这篇文章做个小的总结
拦截器和过滤器的异同
-
相同点
- 都是aop编程思想的体现,可以在程序执行前后做一些操作,如权限操作,日志记录等
-
不同点:
- Filter是Servlet规范中定义的,拦截器是Spring框架中的
- 触发时机不一样,过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的
- 拦截器可以获取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中的拦截器和过滤器小结》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 前端架构之vue+axios 前端实现登录拦截(路由拦截、http拦截)
- react离开页面,自定义弹框拦截,路由拦截
- Springboot整合Hibernate拦截器时无法向拦截器注入Bean
- 基于原生fetch封装一个带有拦截器功能的fetch,类似axios的拦截器
- SpringMVC拦截器
- IOS 拦截器
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
世界是平的(3.0版)
[美] 托马斯·弗里德曼 / 何帆、肖莹莹、郝正非 / 湖南科学技术出版社 / 2008-9 / 58.00元
世界变得平坦,是不是迫使我们跑得更快才能拥有一席之地? 在《世界是平的》中,托马斯·弗里德曼描述了当代世界发生的重大变化。科技和通信领域如闪电般迅速的进步,使全世界的人们可以空前地彼此接近——在印度和中国创造爆炸式增长的财富;挑战我们中的一些人,比他们更快占领地盘。3.0版新增两章,更新了报告和注释方面的内容,这些内容均采自作者考察世界各地特别是整个美国中心地带的见闻,在美国本土,世界的平坦......一起来看看 《世界是平的(3.0版)》 这本书的介绍吧!