SpringBoot学习之路:11.Spring Boot中添加Interceptor应用
2017-07-07 00:00
274 查看
摘要: SpringBoot学习之路之Spring Boot中添加Interceptor应用
前面说了SpringBoot中使用servlet、过滤器(filter)、监听器(Listener),还有在web应用中使用比较多的是拦截器Interceptor,像之前的Strutrs2框架实现的核心就是一套套的拦截器,那么springBoot项目中怎么使用自定义的拦截器呢?
自定义拦截器2:
>>>Interceptor01>>>>>>>在请求处理之前进行调用
>>>Interceptor02>>>>>>>在请求处理之前进行调用
>>>Interceptor02>>>>>>>请求处理之后进行调用
>>>Interceptor01>>>>>>>请求处理之后进行调用
>>>Interceptor02>>>>>>>在整个请求结束之后被调用
>>>Interceptor01>>>>>>>在整个请求结束之后被调用
前面说了SpringBoot中使用servlet、过滤器(filter)、监听器(Listener),还有在web应用中使用比较多的是拦截器Interceptor,像之前的Strutrs2框架实现的核心就是一套套的拦截器,那么springBoot项目中怎么使用自定义的拦截器呢?
1.编写自定义拦截器实现HandlerInterceptor接口
自定义拦截器1:package com.maxbill.core.webbox.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @功能 自定义拦截器 * @作者 zuoshuai(MaxBill) * @日期 2017/7/6 * @时间 15:42 * @备注 只有经过DispatcherServlet的请求,才会走拦截器链, * 我们自定义的Servlet请求是不会被拦截的;Servlet只要符合过滤器的过滤规则,过滤器都会过滤。 */ public class Interceptor01 implements HandlerInterceptor { /** * 在请求处理之前进行调用(Controller方法调用之前) */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(">>>Interceptor01>>>>>>>在请求处理之前进行调用"); return true;// 只有返回true才会继续向下执行,返回false取消当前请求 } /** * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(">>>Interceptor01>>>>>>>请求处理之后进行调用"); } /** * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行 *(主要是用于进行资源清理工作) */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println(">>>Interceptor01>>>>>>>在整个请求结束之后被调用"); } }
自定义拦截器2:
package com.maxbill.core.webbox.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @功能 自定义拦截器 * @作者 zuoshuai(MaxBill) * @日期 2017/7/6 * @时间 15:42 * @备注 只有经过DispatcherServlet的请求,才会走拦截器链,我们自定义的Servlet请求是不会被拦截的;Servlet只要符合过滤器的过滤规则,过滤器都会过滤。 */ public class Interceptor02 implements HandlerInterceptor { /** * 在请求处理之前进行调用(Controller方法调用之前) */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(">>>Interceptor02>>>>>>>在请求处理之前进行调用"); return true;// 只有返回true才会继续向下执行,返回false取消当前请求 } /** * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(">>>Interceptor02>>>>>>>请求处理之后进行调用"); } /** * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println(">>>Interceptor02>>>>>>>在整个请求结束之后被调用"); } }
2.编写WebMvcConfig配置类继承WebMvcConfigurerAdapter
package com.maxbill.core.config.webapp; import com.maxbill.core.webbox.interceptor.Interceptor01; import com.maxbill.core.webbox.interceptor.Interceptor02; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * @功能 * @作者 zuoshuai(MaxBill) * @日期 2017/7/6 * @时间 15:40 * @备注 WebMvcConfig */ @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { /** * 拦截器注册 */ @Override public void addInterceptors(InterceptorRegistry registry) { // 多个拦截器组成一个拦截器链 // addPathPatterns 用于添加拦截规则 // excludePathPatterns 用户排除拦截 registry.addInterceptor(new Interceptor01()).addPathPatterns("/**"); registry.addInterceptor(new Interceptor02()).addPathPatterns("/**"); super.addInterceptors(registry); } }
3.启动项目查看拦截器作用
打印日志如下:>>>Interceptor01>>>>>>>在请求处理之前进行调用
>>>Interceptor02>>>>>>>在请求处理之前进行调用
>>>Interceptor02>>>>>>>请求处理之后进行调用
>>>Interceptor01>>>>>>>请求处理之后进行调用
>>>Interceptor02>>>>>>>在整个请求结束之后被调用
>>>Interceptor01>>>>>>>在整个请求结束之后被调用
4.拦截器疑点说明(说明)
启动项目后,请求昨天编写的servlet,结果发现并没有走我们的拦截器,而是走了我们昨天的过滤器,于是可以得出结论:只有经过DispatcherServlet分发的请求,才会走拦截器链;我们自定义的Servlet请求是不会被拦截的;我们的Servlet只要符合过滤器的过滤规则,过滤器都会过滤。相关文章推荐
- SpringBoot学习之路:10.Spring Boot中添加Listener应用
- SpringBoot学习之路:09.Spring Boot中添加Filter应用
- SpringBoot学习之路:08.Spring Boot中添加Servlet应用
- spring boot 学习之路5(打成war包部署tomcat)
- spring boot 学习之路3( 集成mybatis )
- springboot学习(二)springboot项目整合前端,添加jsp的访问
- SpringBoot学习之路:12.Spring Boot使用idea开发如何配置热加载
- springboot 学习之路 2(注解介绍)
- springboot 学习之路 9 (项目启动后就执行特定方法)
- SpringBoot学习之路:13.SpringBoot使用Lombok简化JavaBean代码
- springboot 学习之路 4(日志输出)
- SpringBoot学习之路:05.Spring Boot集成pagehelper分页插件
- SpringBoot学习之路:01.开发环境搭建及概念叙述
- SpringBoot学习之路:02.第一个程序Hello World及项目结构介绍
- SpringBoot学习之路:07.Spring Boot集成阿里Druid数据源
- springboot 学习之路 15(集成shiro)
- Spring Boot学习之路——自定义拦截器
- SpringBoot学习笔记(4):添加自定义的过滤器
- springboot 学习之路 14(整合mongodb的Api操作)
- spring-boot 学习之路