spring boot 拦截器HandlerInterceptor 不生效的原因排查

java 复制代码
public class UserInterceptor implements HandlerInterceptor

项目添加一个拦截器,发现未生效

1、排查拦截本身是否注入了springbean 容器

java 复制代码
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

2、排查springboot 项目扫描范围是否包含了拦截器所在目录

3、排查拦截器是否注册成功

两种注册方式相互排斥

如果同时配置一个类继承WebMvcConfigurationSupport和一个类实现

WebMvcConfigurer或者WebMvcConfigurerAdapter,就会导致只有一个生效。解决办法:将这些配置都在一个类中设置

这里全局搜索WebMvcConfigurer

和WebMvcConfigurationSuppor

发现项目中已经有了

bash 复制代码
@Configuration
public class FastJsonConfig extends WebMvcConfigurationSupport 

在这里增添

bash 复制代码
@Override
  public 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);
  }

然后打断点,发现项目启动的时候拦截器注册成功,接下来可以正常拿用户登录信息了

相关推荐
海兰2 分钟前
【springboot】gradle快速镜像配置
spring boot·笔记·后端
cheems95273 分钟前
[SpringMVC] Spring MVC 留言板开发实战
java·spring·mvc
BioRunYiXue4 分钟前
AlphaGenome:DeepMind 新作,基因组学迎来 Alpha 时刻
java·linux·运维·网络·数据库·人工智能·eclipse
武超杰7 分钟前
SpringBoot 整合 Spring Security 实现权限控制
spring boot·后端·spring
whatever who cares10 分钟前
android中,全局管理数据/固定数据要不要放一起?
android·java·开发语言
XMYX-014 分钟前
06 - Go 的切片、字典与遍历:从原理到实战
后端·golang
C1829818257515 分钟前
AI idea 集成claude code插件
java·ide·intellij-idea
架构师专栏16 分钟前
比 MQ 更轻的异步方案:Spring 内置的这个隐藏功能,很多人还不知道
后端
IT 行者16 分钟前
解决 IntelliJ IDEA 内存占用高的两个优化策略:GPU 渲染与虚拟内存配置
java·ide·intellij-idea·ai编程
Aric_Jones18 分钟前
从实战理解异步、并发并行与GIL:FastAPI vs SpringBoot
java·spring boot·fastapi