content: package com.open.capacity.security.filter;
  
  import com.open.capacity.security.dto.LoginUser;
  import com.open.capacity.security.service.TokenService;
  import org.apache.commons.lang3.StringUtils;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.web.filter.OncePerRequestFilter;
  
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  /**
   * Token过滤器
   *
   * @author owen 624191343@qq.com
   * <p>
   * 2017年10月14日
   */
  public class TokenFilter extends OncePerRequestFilter {
  
      private static final String TOKEN_KEY = "access_token";
      private static final Long MINUTES_10 = 10 * 60 * 1000L;
      private TokenService tokenService;
      private UserDetailsService userDetailsService;
  
      /**
       * 根据参数或者header获取token
       *
       * @param request
       * @return
       */
      public static String getToken(HttpServletRequest request) {
          String token = request.getParameter(TOKEN_KEY);
          if (StringUtils.isBlank(token)) {
              token = request.getHeader(TOKEN_KEY);
          }
  
          return token;
      }
  
      public TokenService getTokenService() {
          return tokenService;
      }
  
      public void setTokenService(TokenService tokenService) {
          this.tokenService = tokenService;
      }
  
      public UserDetailsService getUserDetailsService() {
          return userDetailsService;
      }
  
      public void setUserDetailsService(UserDetailsService userDetailsService) {
          this.userDetailsService = userDetailsService;
      }
  
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
              throws ServletException, IOException {
          String token = getToken(request);
          if (StringUtils.isNotBlank(token)) {
              LoginUser loginUser = tokenService.getLoginUser(token);
              if (loginUser != null) {
                  loginUser = checkLoginTime(loginUser);
                  UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(loginUser,
                          null, loginUser.getAuthorities());
                  SecurityContextHolder.getContext().setAuthentication(authentication);
              }
          }
  
          filterChain.doFilter(request, response);
      }
  
      /**
       * 校验时间<br>
       * 过期时间与当前时间对比，临近过期10分钟内的话，自动刷新缓存
       *
       * @param loginUser
       * @return
       */
      private LoginUser checkLoginTime(LoginUser loginUser) {
          long expireTime = loginUser.getExpireTime();
          long currentTime = System.currentTimeMillis();
          if (expireTime - currentTime <= MINUTES_10) {
              String token = loginUser.getToken();
              loginUser = (LoginUser) userDetailsService.loadUserByUsername(loginUser.getUsername());
              loginUser.setToken(token);
              tokenService.refresh(loginUser);
          }
          return loginUser;
      }
  
  }
  