zoukankan      html  css  js  c++  java
  • springboot拦截器排除路径失效

    启动程序,需要过滤的路径一直失效。远代码如下:

    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
        @Bean
        public TokenInterceptor tokenInterceptor() {
            return new TokenInterceptor();
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(tokenInterceptor()).addPathPatterns("/**");
            registry.addInterceptor(tokenInterceptor()).excludePathPatterns("/login**");
            registry.addInterceptor(tokenInterceptor()).excludePathPatterns("/swagger**/**");
            super.addInterceptors(registry);
        }
    
    }
    @Component
    public class TokenInterceptor extends HandlerInterceptorAdapter {
    
        @Autowired
        private TokenService validationService;
    
        /**
         * 根据请求不同对token进行处理
         * @param request
         * @param response
         * @param handler
         * @return
         * @throws Exception
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            String accessToken = request.getHeader("token");
            boolean accessFlag = false;
            System.out.println("======"+request.getRequestURI());
            accessFlag = validationService.validateAccessToken(accessToken);
            if(accessFlag){
                validationService.updateToken(accessToken);
            }
            return accessFlag;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            super.postHandle(request, response, handler, modelAndView);
        }
    }

      

    @SpringBootApplication
    @ServletComponentScan
    public class EmployeeApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EmployeeApplication.class, args);
        }
    }
    @ServletComponentScan和@Component注解可以不加
    之后调整了一下WebMvcConfig里代码的写法得以实现,具体原理不是很清楚。
    调整后代码如下:
    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
        @Bean
        public TokenInterceptor tokenInterceptor() {
            return new TokenInterceptor();
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(tokenInterceptor()).excludePathPatterns("/users/login**","/swagger**/**").addPathPatterns("/**");
            super.addInterceptors(registry);
        }
    
    }
  • 相关阅读:
    【idea】批量修改文件的换行类型
    【shell】for循环执行sql语句
    【gitlab】创建token供外部git clone
    【idea】修改git源
    【浏览器】 NET::ERR_CERT_INVALID
    mac os 11 Big Sur 根目录无法写入解决办法
    dbeaver把表数据导出csv时字符串自动加双引号问题解决
    spring boot2 jpa分页查询百万级数据内存泄漏
    win10安装MAT并单独配置jdk11
    Transaction silently rolled back because it has been marked as rollback-only
  • 原文地址:https://www.cnblogs.com/soul-mate/p/7852020.html
Copyright © 2011-2022 走看看