• IntelliJ IDEA 2017版 spring-boot搭建拦截器


    1、建立一个springboot-web项目

             https://www.cnblogs.com/liuyangfirst/p/8298588.html

    2、加入过滤接口

     1 public class LoginInterceptor implements HandlerInterceptor {
     2 
     3 
     4     @Override
     5     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     6 
     7         System.out.println("已经进入了登录拦截器......");
     8 
     9         // 逻辑代码按照之前的方式去编写即可
    10 
    11 
    12         return true;
    13     }
    14 
    15     @Override
    16     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    17 
    18     }
    19 
    20     @Override
    21     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    22 
    23     }
    24 }
    View Code

    3、配置识别类

     1 // 当前类变成配置拦截器类
     2 @Configuration
     3 public class WebConfig extends WebMvcConfigurerAdapter {
     4 
     5     @Override
     6     public void addInterceptors(InterceptorRegistry registry) {
     7 
     8         // 需要拦截的路径
     9         String[] addPathPatterns = {"/test/**"};
    10 
    11         //不拦截的路径
    12         String[] excludePathPatterns = {"/index", "/myjsp"};
    13 
    14 
    15 //        注册登录拦截器(此拦截器注册多行就是,添加多个拦截器)
    16         registry.addInterceptor(new LoginInterceptor())
    17                 .addPathPatterns(addPathPatterns).
    18                 excludePathPatterns(excludePathPatterns);
    19     }
    20 }
    View Code

    4、源码位置

    https://github.com/liushaoye/05-filter/tree/master

  • 相关阅读:
    [leetCode]09.用两个栈实现队列
    ubuntu:无法获得锁;无法锁定管理目录
    [leetCode]07.重建二叉树
    [leetCode]剑指 Offer 06. 从尾到头打印链表
    [leetCode]剑指 Offer 05. 替换空格
    [leetCode]1330.翻转子数组得到最大的数组值
    [leetCode]312.戳气球
    UVALive
    CodeChef
    CodeChef
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/9316557.html
Copyright © 2020-2023  润新知