java - SpringBoot 添加自定义的拦截器,却不调用
PHP中文网
PHP中文网 2017-06-30 09:54:03
0
4
965

1.问题:

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

2.代码:
自定义的HandlerInterceptorAdapter的AccessTokenVerifyInterceptor:

@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; } }

自定义的WebMvcConfigurerAdapter类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.希望各路大神,帮小弟指点指点,感谢感谢!

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复 (4)
Peter_Zhu

/**去掉一个*试试

    扔个三星炸死你

    在启动类加上
    @ServletComponentScan

      阿神

      那你应该把 HandlerInterceptorAdapter 贴出来看看
      还有就是 2 中那个 @Component 注解 是没有意义的。

        学霸

        不会啊,按照你的代码写了一个简单的Demo,拦截器是可以调用到的。

        @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; } }

          最新下载
          更多>
          网站特效
          网站源码
          网站素材
          前端模板
          关于我们 免责声明 Sitemap
          PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!