content: package com.achobeta.intercepter;
  
  import cn.hutool.core.util.IdUtil;
  import org.slf4j.MDC;
  import org.springframework.stereotype.Component;
  import org.springframework.web.servlet.HandlerInterceptor;
  import org.springframework.web.servlet.ModelAndView;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * @author BanTanger 半糖
   * @date 2024/11/3
   */
  
  @Component
  public class LogInterceptor implements HandlerInterceptor {
  
      public static final String TRACE_ID = "traceId";
  
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
          //如果有上层调用就用上层的ID
          String traceId = request.getHeader(TRACE_ID);
          if (traceId == null) {
              traceId = IdUtil.objectId();
          }
          MDC.put(TRACE_ID, traceId);
          response.addHeader(TRACE_ID, traceId);
          return true;
      }
      @Override
      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
              throws Exception {
      }
      @Override
      public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
              throws Exception {
          MDC.remove(TRACE_ID);
      }
  
  }
  