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.希望各路大神,幫小弟指點指點,感謝感謝!
/**去掉一個*試試
在啟動類別中加上
@ServletComponentScan
那你應該把 HandlerInterceptorAdapter 貼出來看看
還有就是 2 中那個 @Component 註解 是沒有意義的。
不會啊,按照你的程式碼寫了一個簡單的Demo,攔截器是可以呼叫的。