content: package com.xiliu.server.common.security.filter;
  
  
  
  import com.xiliu.server.common.component.JwtToken;
  import com.xiliu.server.common.domain.LoginUser;
  import com.xiliu.server.common.security.entity.JwtUser;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
  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;
  
  /**
   * @author xiliu
   * @description JWT登录授权过滤器
   * @date 2021/9/6
   */
  @Slf4j
  public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
  
  
      @Value("${jwt.tokenHeader}")
      private String tokenHeader;
      @Value("${jwt.tokenHead}")
      private String tokenHead;
      @Autowired
      private JwtToken jwtToken;
  
      @Override
      protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
          //JwtUser jwtUser = jwtToken.getLoginUser(httpServletRequest);
          LoginUser jwtUser = jwtToken.getNewLoginUser(httpServletRequest);
          if (null != jwtUser && null == SecurityContextHolder.getContext().getAuthentication()) {
              jwtToken.verifyToken(jwtUser);
              UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
              authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
              SecurityContextHolder.getContext().setAuthentication(authentication);
          }
          filterChain.doFilter(httpServletRequest, httpServletResponse);
      }
  }
  