content: package com.weiguanjishu.filter;
  
  import io.seata.core.context.RootContext;
  import org.apache.commons.lang.StringUtils;
  import org.springframework.stereotype.Component;
  
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import java.io.IOException;
  
  @Component
  public class SeataFilter implements Filter {
      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
      }
  
      @Override
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          HttpServletRequest req = (HttpServletRequest) servletRequest;
          String xid = req.getHeader(RootContext.KEY_XID);
          boolean isBind = false;
          if (StringUtils.isNotBlank(xid)) {
              RootContext.bind(xid);
              isBind = true;
          }
          try {
              filterChain.doFilter(servletRequest, servletResponse);
          } finally {
              if (isBind) {
                  RootContext.unbind();
              }
          }
      }
  
      @Override
      public void destroy() {
      }
  }
  