content: 
  package cn.datax.service.data.metadata.security;
  
  import cn.datax.common.redis.service.RedisService;
  import cn.datax.service.data.metadata.model.OnlineUserDto;
  import cn.hutool.core.util.StrUtil;
  
  import io.jsonwebtoken.ExpiredJwtException;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.scheduling.annotation.Async;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.util.StringUtils;
  import org.springframework.web.filter.GenericFilterBean;
  
  import javax.annotation.Resource;
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import java.io.IOException;
  import java.util.Objects;
  
  /**
   * @author AllDataDC
   */
  public class TokenFilter extends GenericFilterBean {
      private static final Logger log = LoggerFactory.getLogger(TokenFilter.class);
  
      private final TokenProvider tokenProvider;
      private final SecurityProperties properties;
      @Resource
      private RedisService redisUtils;
  
      /**
       * @param tokenProvider     Token
       * @param properties        JWT
       */
      public TokenFilter(TokenProvider tokenProvider, SecurityProperties properties) {
          this.properties = properties;
          this.tokenProvider = tokenProvider;
      }
  
      @Override
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
              throws IOException, ServletException {
          HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
          String token = resolveToken(httpServletRequest);
          // 对于 Token 为空的不需要去查 Redis
          if (StrUtil.isNotBlank(token)) {
              OnlineUserDto onlineUserDto = null;
              boolean cleanUserCache = false;
              try {
                  onlineUserDto = getOne(properties.getOnlineKey() + token);
              } catch (ExpiredJwtException e) {
                  log.error(e.getMessage());
                  cleanUserCache = true;
              } finally {
                  if (cleanUserCache || Objects.isNull(onlineUserDto)) {
                      cleanUserCache(String.valueOf(tokenProvider.getClaims(token).get(TokenProvider.AUTHORITIES_KEY)));
                  }
              }
              if (onlineUserDto != null && StringUtils.hasText(token)) {
                  Authentication authentication = tokenProvider.getAuthentication(token);
                  SecurityContextHolder.getContext().setAuthentication(authentication);
                  // Token 续期
                  tokenProvider.checkRenewal(token);
              }
          }
          filterChain.doFilter(servletRequest, servletResponse);
      }
  
      /**
       * 初步检测Token
       *
       * @param request /
       * @return /
       */
      private String resolveToken(HttpServletRequest request) {
          String bearerToken = request.getHeader(properties.getHeader());
          if (StringUtils.hasText(bearerToken) && bearerToken.startsWith(properties.getTokenStartWith())) {
              // 去掉令牌前缀
              return bearerToken.replace(properties.getTokenStartWith(), "");
          } else {
              log.debug("非法Token：{}", bearerToken);
          }
          return null;
      }
  
      /**
       * 查询用户
       * @param key /
       * @return /
       */
      public OnlineUserDto getOne(String key) {
          return (OnlineUserDto)redisUtils.get(key);
      }
  
      /**
       * 清理用户缓存信息
       * 用户信息变更时
       * @param userName 用户名
       */
      @Async
      public void cleanUserCache(String userName) {
          if (!StringUtils.isEmpty(userName)) {
              // 清除数据
              redisUtils.hdel("USER-LOGIN-DATA", userName);
          }
      }
  }
  