为什么拦截器运行三次


public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) {
String jwt = request.getHeader("auth");
String payloadKey = "apitest";
HandlerMethod handlerMethod=(HandlerMethod)object;
Class type = handlerMethod.getBeanType();
if (type.isAnnotationPresent(Auth.class)) {
try {
if (jwt == null || !Objects.equals(payloadKey, JwtUtil.parseJWT(jwt).get("info", String.class))) {
return false;
}
}catch (ExpiredJwtException | SignatureException | MalformedJwtException e){
return false;
}
}
log.info("1");
return true;
}

当jwt是真的,但我看到log.info("1"(运行了3次,为什么它运行了3倍?

我的拦截器配置:

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authenticationInterceptor())
.addPathPatterns("/**");
}
@Bean
public AuthenticationInterceptor authenticationInterceptor() {
return new AuthenticationInterceptor();
}

我在控制器类上设置了@Auth:@Auth公共类ApiController

当我清除我的preHandle时,排除了运行3次的"log.info("1"(return true">

更新

您可以调试到AuthenticationInterceptor中,以确认以下内容:

  1. 如果handlerMethod总共指向不同的对象3次?

  2. AuthenticationInterceptor,即this变量,是否不同?

  3. 如果你的请求有重定向?

我试过使用一些像你这样的代码,它的行为是正确的。你能展示更多的代码或简单的方法来重现这个问题吗?

最新更新