1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| @Slf4j public class JwtAuthenticationFilter extends BasicAuthenticationFilter {
private final RedisService redisService; public JwtAuthenticationFilter(AuthenticationManager authenticationManager, RedisService redisService) { super(authenticationManager); this.redisService = redisService; }
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { UsernamePasswordAuthenticationToken authentication = getAuthentication(request, response); SecurityContextHolder.getContext().setAuthentication(authentication); chain.doFilter(request, response); }
private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request, HttpServletResponse response) {
String token = request.getHeader("Authorization"); if (StringUtils.hasLength(token)) { String cacheToken = String.valueOf(redisService.get(token)); if (StringUtils.hasLength(token) && !"null".equals(cacheToken)) { String user = null; try { Claims claims = Jwts.parser() .setSigningKey(ConstantKey.SIGNING_KEY) .parseClaimsJws(cacheToken).getBody(); user = claims.getSubject(); resetRedisExpire(token, claims); } catch (ExpiredJwtException e) { log.info("Token过期续签,ExpiredJwtException={}", e.getMessage()); Claims claims = e.getClaims(); user = claims.getSubject(); refreshToken(token, claims); } catch (UnsupportedJwtException e) { log.warn("访问[{}]失败,UnsupportedJwtException={}", request.getRequestURI(), e.getMessage()); } catch (MalformedJwtException e) { log.warn("访问[{}]失败,MalformedJwtException={}", request.getRequestURI(), e.getMessage()); } catch (SignatureException e) { log.warn("访问[{}]失败,SignatureException={}", request.getRequestURI(), e.getMessage()); } catch (IllegalArgumentException e) { log.warn("访问[{}]失败,IllegalArgumentException={}", request.getRequestURI(), e.getMessage()); } if (user != null) { String[] split = user.split("-")[1].split(","); ArrayList<GrantedAuthority> authorities = new ArrayList<>(); for (String s : split) { authorities.add(new GrantedAuthorityImpl(s)); } return new UsernamePasswordAuthenticationToken(user, null, authorities); } } } log.warn("访问[{}]失败,需要身份认证", request.getRequestURI()); return null; }
private void resetRedisExpire(String token, Claims claims) { long current = System.currentTimeMillis(); long issuedAt = claims.getIssuedAt().getTime(); long expiration = claims.getExpiration().getTime(); long expireAt = current + (expiration - issuedAt); redisService.expire(token, expireAt); }
private void refreshToken(String token, Claims claims) { long current = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); Date now = calendar.getTime(); calendar.add(Calendar.MINUTE, 5); Date time = calendar.getTime(); String refreshToken = Jwts.builder() .setSubject(claims.getSubject()) .setIssuedAt(now) .setExpiration(time) .signWith(SignatureAlgorithm.HS512, ConstantKey.SIGNING_KEY) .compact(); redisService.set(token, refreshToken, time); log.info("刷新token执行时间: {}", (System.currentTimeMillis() - current) + " 毫秒"); } }
|