springboot拦截器HandlerInterceptor实现与配置
场景:springboot拦截器HandlerInterceptor实现与配置
记录:NO.244
本例环境:
apache-tomcat-8.5.57
jdk:1.8
名词:
1.HandlerInterceptor包括preHandle()、postHandle()、afterCompletion()
preHandle():请求进入Controller前调用。
postHandle():Controller的方法调用之后执行,在DispatcherServlet进行视图返回渲染之前被调用,所以可以在这个方法中对Controller处理之后的ModelAndView对象进行操作。
afterCompletion():在请求处理后,在DispatcherServlet渲染了对应的视图之后执行,进行资源清理工作。
2.拦截器是在Spring容器内组件,是Spring框架支持的组件。配置在SpringMVC中的。
一、springboot版本
1.配置拦截器
@Configuration
public class PlatformWebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
FirstHandlerInterceptor first = new FirstHandlerInterceptor();
SecondHandlerInterceptor second = new SecondHandlerInterceptor();
ThirdHandlerInterceptor third = new ThirdHandlerInterceptor();
registry.addInterceptor(first).addPathPatterns("/**");
registry.addInterceptor(second).addPathPatterns("/**");
registry.addInterceptor(third).addPathPatterns("/**");
}
}
2.FirstHandlerInterceptor
public class FirstHandlerInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstHandlerInterceptor->preHandle执行......");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
StringBuffer urlBuffer = request.getRequestURL();
System.out.println("FirstHandlerInterceptor->postHandle执行......");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
StringBuffer urlBuffer = request.getRequestURL();
System.out.println("FirstHandlerInterceptor->afterCompletion执行......");
}
}
3.SecondHandlerInterceptor
public class SecondHandlerInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("SecondHandlerInterceptor->preHandle执行......");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("SecondHandlerInterceptor->postHandle执行......");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("SecondHandlerInterceptor->afterCompletion执行......");
}
}
4.SecondHandlerInterceptor
public class ThirdHandlerInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("ThirdHandlerInterceptor->preHandle执行......");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("ThirdHandlerInterceptor->postHandle执行......");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("ThirdHandlerInterceptor->afterCompletion执行......");
}
}
5.测试Controller
@RestController
@RequestMapping("/fujian")
public class PlatformController {
@GetMapping("/shangban")
public String getShangBan(String num) {
System.out.println("PlatformController->getShangBan执行......");
System.out.println("开始上班......");
System.out.println("上班中......");
System.out.println("报酬 num = " + num + "万");
System.out.println("下班回家......");
return "Hey,GoodJob.";
}
}
6.测试
测试URL: http://localhost:8080/platform/fujian/shangban?num=64
测试日志:
二、xml配置版本
1.MVC的XML配置新增配置,其它和以上一样。
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.zbz.interceptor.FirstHandlerInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.zbz.interceptor.SecondHandlerInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.zbz.interceptor.ThirdHandlerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
以上,感谢。
上一篇: CPU颗数 核数 线程数
下一篇: 基于Spring开发之自定义标签及其解析