zoukankan      html  css  js  c++  java
  • SpringBoot2.X自定义拦截器实战及新旧配置对比(核心知识)

    简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解

    1、@Configuration
      继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本)

      SpringBoot2.X 新版本配置拦截器 implements WebMvcConfigurer

    2、自定义拦截器 HandlerInterceptor
      preHandle:调用Controller某个方法之前
      postHandle:Controller之后调用,视图渲染之前,如果控制器Controller出现了异常,则不会执行此方法
      afterCompletion:不管有没有异常,这个afterCompletion都会被调用,用于资源清理

    3、按照注册顺序进行拦截,先注册,先被拦截

      拦截器不生效常见问题:
        1)是否有加@Configuration
        2)拦截路径是否有问题 ** 和 *
        3)拦截器最后路径一定要 “/**”, 如果是目录的话则是 /*/

      Filter
        是基于函数回调 doFilter(),而Interceptor则是基于AOP思想
        Filter在只在Servlet前后起作用,而Interceptor够深入到方法前后、异常抛出前后等

        依赖于Servlet容器即web应用中,而Interceptor不依赖于Servlet容器所以可以运行在多种环境。

        在接口调用的生命周期里,Interceptor可以被多次调用,而Filter只能在容器初始化时调用一次。

        Filter和Interceptor的执行顺序

        过滤前->拦截前->action执行->拦截后->过滤后

    代码示例:

      CustomWebMvcConfigurer.java

     1 package net.xdclass.demo.intecpter;
     2 
     3 import org.springframework.context.annotation.Configuration;
     4 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
     5 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
     6 
     7 
     8 @Configuration
     9 public class CustomWebMvcConfigurer implements WebMvcConfigurer  {
    10 
    11     @Override
    12     public void addInterceptors(InterceptorRegistry registry) {
    13 
    14         //'/*'匹配一个文件夹
    15         registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api2/*/**");
    16         registry.addInterceptor(new TwoIntercepter()).addPathPatterns("/api2/*/**");
    17         
    18         //.excludePathPatterns("/api2/xxx/**"); //拦截全部 /*/*/**
    19         
    20         WebMvcConfigurer.super.addInterceptors(registry);
    21     }
    22 
    23     
    24     
    25     
    26 
    27 
    28 }

    LoginIntercepter.java:

     1 package net.xdclass.demo.intecpter;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 
     6 import org.springframework.web.servlet.HandlerInterceptor;
     7 import org.springframework.web.servlet.ModelAndView;
     8 
     9 public class LoginIntercepter implements HandlerInterceptor{
    10 
    11     /**
    12      * 进入controller方法之前
    13      */
    14     @Override
    15     public boolean preHandle(HttpServletRequest request,
    16             HttpServletResponse response, Object handler) throws Exception {
    17         System.out.println("LoginIntercepter------->preHandle");
    18 
    19 //        String token = request.getParameter("access_token");
    20         
    21 //        response.getWriter().print("fail");
    22         
    23         return HandlerInterceptor.super.preHandle(request, response, handler);
    24     }
    25 
    26     /**
    27      * 调用完controller之后,视图渲染之前
    28      */
    29     @Override
    30     public void postHandle(HttpServletRequest request,
    31             HttpServletResponse response, Object handler,
    32             ModelAndView modelAndView) throws Exception {
    33         
    34         System.out.println("LoginIntercepter------->postHandle");
    35         
    36         HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    37     }
    38 
    39     /**
    40      * 整个完成之后,通常用于资源清理
    41      */
    42     @Override
    43     public void afterCompletion(HttpServletRequest request,
    44             HttpServletResponse response, Object handler, Exception ex)
    45             throws Exception {
    46         System.out.println("LoginIntercepter------->afterCompletion");
    47         
    48         HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    49     }
    50 
    51     
    52     
    53 }

    TwoIntercepter.java:

     1 package net.xdclass.demo.intecpter;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 
     6 import org.springframework.web.servlet.HandlerInterceptor;
     7 import org.springframework.web.servlet.ModelAndView;
     8 
     9 public class TwoIntercepter implements HandlerInterceptor{
    10 
    11     /**
    12      * 进入对应的controller方法之前
    13      */
    14     @Override
    15     public boolean preHandle(HttpServletRequest request,
    16             HttpServletResponse response, Object handler) throws Exception {
    17 
    18         System.out.println("TwoIntercepter------>preHandle");
    19         return HandlerInterceptor.super.preHandle(request, response, handler);
    20     }
    21 
    22     /**
    23      * controller处理之后,返回对应的视图之前
    24      */
    25     @Override
    26     public void postHandle(HttpServletRequest request,
    27             HttpServletResponse response, Object handler,
    28             ModelAndView modelAndView) throws Exception {
    29         System.out.println("TwoIntercepter------>postHandle");
    30         HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    31     }
    32 
    33     /**
    34      * 整个请求结束后调用,视图渲染后,主要用于资源的清理
    35      */
    36     @Override
    37     public void afterCompletion(HttpServletRequest request,
    38             HttpServletResponse response, Object handler, Exception ex)
    39             throws Exception {
    40         System.out.println("TwoIntercepter------>afterCompletion");
    41         HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    42     }
    43 
    44     
    45 }

    测试代码:

    浏览器访问:http://localhost:8083/api2/v1/account

    控制台查看:

  • 相关阅读:
    [CodeIgniter4]-将控制器放入子目录中
    [CodeIgniter4]-控制器
    [CodeIgniter4]-处理多环境
    [CodeIgniter4]-管理多个应用
    [CodeIgniter4]-代码模块
    [CodeIgniter4]-网页缓存
    [CodeIgniter4]-错误处理
    netfilter分析
    使用 SELinux 和 Smack 增强轻量级容器
    如何增强 Linux 系统的安全性,第一部分: Linux 安全模块(LSM)简介
  • 原文地址:https://www.cnblogs.com/116970u/p/10251388.html
Copyright © 2011-2022 走看看