public class UserInterceptor implements HandlerInterceptor
項目添加一個攔截器,發現未生效
1、排查攔截本身是否注入了springbean 容器
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
2、排查springboot 項目掃描范圍是否包含了攔截器所在目錄
3、排查攔截器是否注冊成功
兩種注冊方式相互排斥
如果同時配置一個類繼承WebMvcConfigurationSupport和一個類實現
WebMvcConfigurer或者WebMvcConfigurerAdapter,就會導致只有一個生效。解決辦法:將這些配置都在一個類中設置
這里全局搜索WebMvcConfigurer
和WebMvcConfigurationSuppor
發現項目中已經有了
@Configuration
public class FastJsonConfig extends WebMvcConfigurationSupport
在這里增添
@Overridepublic void addInterceptors(InterceptorRegistry registry) {List<String> excluded=new ArrayList<>();excluded.add("/pc/getUserInfoByCode");excluded.add("pc/login");registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(excluded);super.addInterceptors(registry);}
然后打斷點,發現項目啟動的時候攔截器注冊成功,接下來可以正常拿用戶登錄信息了