search

Home  >  Q&A  >  body text

java - SpringBoot adds a custom interceptor but does not call it

1. Question:

要添加一个自定义处理Token的问题,现在实现了方法,却发现拦截器没有被调用。
我是在自定义的HandlerInterceptorAdapter里面重写了preHandle方法。并把这个自定义的HandlerInterceptorAdapter添加到了自定义的WebMvcConfigurerAdapter,在WebMvcConfigurerAdapter添加@Configuration注解,但是却没有被调用!

2. Code:
AccessTokenVerifyInterceptor of custom HandlerInterceptorAdapter:

@Component
public class AccessTokenVerifyInterceptor extends HandlerInterceptorAdapter {
    
    private Logger logger = LoggerFactory.getLogger(AccessTokenVerifyInterceptor.class);
    
    @Autowired
    private FFAccessTokenService tokenService;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // TODO Auto-generated method stub
        
        logger.info("AccessToken executing ...");
        return true;    
    }
    
}

Customized WebMvcConfigurerAdapter class FFWebMvcConfigurer:

@Configuration
public class FFWebMvcConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // TODO Auto-generated method stub
        registry.addViewController("/error").setViewName("404.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE); 
        super.addViewControllers(registry);    
    }
        
     @Override 
     public void configurePathMatch(PathMatchConfigurer configurer) { 
         configurer.setUseSuffixPatternMatch(false); 
         super.configurePathMatch(configurer); 
     }
     
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         
         registry.addInterceptor(new AccessTokenVerifyInterceptor())
                  .addPathPatterns("/**")
                  .excludePathPatterns("/access-token");
         
         super.addInterceptors(registry);
         
         System.out.println("开始开始咯。。。。");    
    } 
}

3. I hope that all the masters can give me some advice, thank you!

PHP中文网PHP中文网2707 days ago1040

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-06-30 09:56:03

    /**Remove one* and try it

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-30 09:56:03

    Add
    @ServletComponentScan

    to the startup class

    reply
    0
  • 阿神

    阿神2017-06-30 09:56:03

    Then you should post the HandlerInterceptorAdapter and take a look
    Also, the @Component annotation in 2 is meaningless.

    reply
    0
  • 欧阳克

    欧阳克2017-06-30 09:56:03

    No, I wrote a simple demo based on your code, and the interceptor can be called.

    @Configuration
    public class FFWebMvcConfigurer extends WebMvcConfigurerAdapter {
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/error").setViewName("404.html");
            registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
            super.addViewControllers(registry);
        }
    
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            configurer.setUseSuffixPatternMatch(false);
            super.configurePathMatch(configurer);
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    
            registry.addInterceptor(new AccessTokenVerifyInterceptor())
                    .addPathPatterns("/**")
                    .excludePathPatterns("/access-token");
    
            super.addInterceptors(registry);
    
            System.out.println("开始开始咯。。。。");
        }
    
    }
    
    public class AccessTokenVerifyInterceptor extends HandlerInterceptorAdapter {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("preHandle...");
            return true;
        }
    
    }

    reply
    0
  • Cancelreply