浅谈SpringMVC HandlerInterceptor诡异问题排查

作者:戒嗔 时间:2023-07-24 05:34:06 

发现问题

最近在进行压测发现,有一些接口时好时坏,通过sentry日志平台及sky walking平台跟踪发现,用户张三获取到的用户上下文确是李四。

代码走读

用户登录下上文


/**
* 用户登录下上文
*
* @author : jamesfu
* @date : 22/5/2019
* @time : 9:18 AM
*/
@Data
public class UserContext {
 private final static ThreadLocal<UserContext> threadLocal = new ThreadLocal<>();

private Long id;

private String loginName;

public static UserContext get() {
   UserContext context = threadLocal.get();
   if (context == null) {
     // TODO(james.h.fu):根据请求上下文获取token, 然后恢复用户登录下上文
     context = new UserContext() {{
       setId(1L);
       setLoginName("james.h.fu1");
     }};
     threadLocal.set(context);
   }

return context;
 }

public static void clear() {
   threadLocal.remove();
 }

public static void set(UserContext context) {
   if (context != null) {
     threadLocal.set(context);
   }
 }
}

在 * 中有调用UserContext.set恢复用户登录上下文,并在请求结束时调用UserContext.clear清理用户登录上下文。

浅谈SpringMVC HandlerInterceptor诡异问题排查

* 注册配置


/**
* * 注册配置
*
* @author : jamesfu
* @date : 22/5/2019
* @time : 9:15 AM
*/
@Configuration
public class FilterConfig implements WebMvcConfigurer {
 @Autowired
 private JsonRpcInterceptor jsonRpcInterceptor;

@Override
 public void addInterceptors(InterceptorRegistry registry) {
   registry.addInterceptor(jsonRpcInterceptor)
       .addPathPatterns("/json.rpc");
 }
}

浅谈SpringMVC HandlerInterceptor诡异问题排查 

浅谈SpringMVC HandlerInterceptor诡异问题排查 

浅谈SpringMVC HandlerInterceptor诡异问题排查

通过debug可以发现UserContext中的ThreadLocal的清理工作没有得到执行。导致请求进来时,有可能ThreadLocal已存在了,就不会再根据请求上下文恢复了。

springmvc 源码走读

tomcat 在收到http请求后,最终会交由spring mvc的 DispatcherServlet 处理。 这里可以从doDispatch按图索骥,顺藤摸瓜地往下看起走。

源码走读:DispatcherServlet


/**
* Process the actual dispatching to the handler.
* <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
* The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
* to find the first that supports the handler class.
* <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
* themselves to decide which methods are acceptable.
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception in case of any kind of processing failure
*/
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception

请求会得到分发,然后执行各个已注册Handler的preHandle-->postHandle-->afterCompletion。

源码走读:HandlerExecutionChain applyPreHandle


/**
* Apply preHandle methods of registered interceptors.
* @return {@code true} if the execution chain should proceed with the
* next interceptor or the handler itself. Else, DispatcherServlet assumes
* that this interceptor has already dealt with the response itself.
*/
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = 0; i < interceptors.length; i++) {
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(request, response, this.handler)) {
triggerAfterCompletion(request, response, null);
return false;
}
this.interceptorIndex = i;
}
}
return true;
}

当执行到preHandle返回false时,它就会从上一个返回true的handler依次往前执行afterCompletion,它自己的afterCompletion得不到执行。

triggerAfterCompletion


/**
* Trigger afterCompletion callbacks on the mapped HandlerInterceptors.
* Will just invoke afterCompletion for all interceptors whose preHandle invocation
* has successfully completed and returned true.
*/
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable Exception ex)
throws Exception {

HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = this.interceptorIndex; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
try {
interceptor.afterCompletion(request, response, this.handler, ex);
}
catch (Throwable ex2) {
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
}
}
}
}

triggerAfterCompletion只会在(1)出现异常,(2)preHandle返回false 或(3)正常执行结束才会从索引interceptorIndex依次往前执行。

所以基于以上源码可以得知,在写 * 时preHandle返回false时,afterCompletion是不会执行的。所以一些必要的清理工作得不到执行,会出现类似我们遇到的帐号串的问题。

来源:https://juejin.im/post/5ce4dfdb6fb9a07f04201f0c

标签:SpringMVC,HandlerInterceptor
0
投稿

猜你喜欢

  • Java jar打包工具使用方法步骤解析

    2023-07-01 12:26:47
  • Java Lambda List转Map代码实例

    2022-05-24 20:15:42
  • C# 设计模式系列教程-状态模式

    2022-11-07 13:31:55
  • Android代码实现新年贺卡动画示例详解

    2022-09-11 00:28:49
  • C#学习笔记整理_浅谈Math类的方法

    2022-04-11 06:16:48
  • 利用Flutter实现“孔雀开屏”的动画效果

    2021-11-04 21:24:17
  • AndroidStudio Gradle基于友盟的多渠道打包方法

    2022-02-24 22:04:43
  • Android 中CheckBox多项选择当前的position信息提交的示例代码

    2022-05-15 19:13:51
  • JAVA字符串拼接常见方法汇总

    2021-11-13 03:11:31
  • 创建动态代理对象bean,并动态注入到spring容器中的操作

    2021-09-04 01:02:43
  • 小议Java中final关键字使用时的注意点

    2022-12-04 08:40:45
  • Android使用开源组件PagerBottomTabStrip实现底部菜单和顶部导航功能

    2023-07-02 11:32:40
  • SpringBoot2使用Jetty容器操作(替换默认Tomcat)

    2023-11-24 01:17:15
  • Ajax实现省市区三级联动

    2023-01-14 05:09:58
  • 详解C# 泛型中的数据类型判定与转换

    2023-05-03 08:08:36
  • java内存模型jvm虚拟机简要分析

    2022-09-08 09:29:34
  • Java实现酒店客房管理系统

    2023-11-21 06:58:38
  • Spark SQL配置及使用教程

    2023-02-28 06:02:30
  • IDEA2020.1常用配置说明

    2023-01-09 02:11:50
  • 解读java try catch 异常后还会继续执行吗

    2022-05-11 01:25:24
  • asp之家 软件编程 m.aspxhome.com