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

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

相关推荐
野犬寒鸦5 分钟前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
七夜zippoe13 分钟前
AI+Java 守护你的钱袋子!金融领域的智能风控与极速交易
java·人工智能·金融
岁忧25 分钟前
(LeetCode 面试经典 150 题) 200. 岛屿数量(深度优先搜索dfs || 广度优先搜索bfs)
java·c++·leetcode·面试·go·深度优先
phiilo30 分钟前
golang 设置进程退出时kill所有子进程
后端
花花无缺31 分钟前
python自动化-pytest-用例发现规则和要求
后端·python
程序员小假33 分钟前
我们来说一说 Cglib 与 JDK 动态代理
后端
liliangcsdn1 小时前
结合prompt分析NodeRAG的build过程
java·服务器·人工智能·数据分析·知识图谱
黑色的山岗在沉睡1 小时前
LeetCode 189. 轮转数组
java·算法·leetcode
会飞的小蛮猪2 小时前
Jenkins运维之路(权限分配&忘记admin密码)
java·运维·经验分享·jenkins·prometheus