拦截器

拦截器代码实现

[第一步]——定义一个类,实现HandlerInterceptor接口即可

  1. @Component //注意当前类必须受Spring容器控制
  2. //定义拦截器类,实现HandlerInterceptor接口
  3. public class ProjectInterceptor implements HandlerInterceptor {
  4. @Override
  5. //原始方法调用前执行的内容
  6. //返回值类型可以拦截控制的执行,true放行,false终止
  7. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  8. System.out.println("preHandle..."+contentType);
  9. return true;
  10. }
  11. @Override
  12. //原始方法调用后执行的内容
  13. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  14. System.out.println("postHandle...");
  15. }
  16. @Override
  17. //原始方法调用完成后执行的内容
  18. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  19. System.out.println("afterCompletion...");
  20. }
  21. }

参数

  1. request:请求对象
  2. response:响应对象
  3. handler:被调用的处理器对象,本质上是一个方法对象,对反射技术中的Method对象进行了再包装

[第二步]———配置加载拦截器

第一种

  1. @Configuration
  2. public class SpringMvcSupport extends WebMvcConfigurationSupport {
  3. @Autowired
  4. private ProjectInterceptor projectInterceptor;
  5. @Override
  6. protected void addInterceptors(InterceptorRegistry registry) {
  7. //配置拦截器
  8. registry.addInterceptor(projectInterceptor)
  9. .addPathPatterns("/books","/books/*");
  10. }
  11. }

第二种

使用标准接口WebMvcConfigurer简化开发(注意:侵入式较强)

  1. @Configuration
  2. @ComponentScan({"com.itheima.controller"})
  3. @EnableWebMvc
  4. //实现WebMvcConfigurer接口可以简化开发,但具有一定的侵入性
  5. public class SpringMvcConfig implements WebMvcConfigurer {
  6. @Autowired
  7. private ProjectInterceptor projectInterceptor;
  8. @Override
  9. public void addInterceptors(InterceptorRegistry registry) {
  10. //配置多拦截器
  11. registry.addInterceptor(projectInterceptor)
  12. .addPathPatterns("/books","/books/*");
  13. }
  14. }

———可定义多个拦截器

例如

第二个拦截器

  1. @Component
  2. public class ProjectInterceptor2 implements HandlerInterceptor {
  3. @Override
  4. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  5. System.out.println("preHandle...222");
  6. return false;
  7. }
  8. @Override
  9. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  10. System.out.println("postHandle...222");
  11. }
  12. @Override
  13. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  14. System.out.println("afterCompletion...222");
  15. }
  16. }

配置

  1. @Configuration
  2. @ComponentScan({"com.itheima.controller"})
  3. @EnableWebMvc
  4. //实现WebMvcConfigurer接口可以简化开发,但具有一定的侵入性
  5. public class SpringMvcConfig implements WebMvcConfigurer {
  6. @Autowired
  7. private ProjectInterceptor projectInterceptor;
  8. @Autowired
  9. private ProjectInterceptor2 projectInterceptor2;
  10. @Override
  11. public void addInterceptors(InterceptorRegistry registry) {
  12. //配置多拦截器
  13. registry.addInterceptor(projectInterceptor)
  14. .addPathPatterns("/books","/books/*");
  15. registry.addInterceptor(projectInterceptor2)
  16. .addPathPatterns("/books","/books/*");
  17. }
  18. }

执行顺序

image-20210805175445422.png

执行流程展示

image-20210805180846313.png