@Slf4j @Component public class SysUtils { public static Claims getCurrentLoginUser() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String authorization = request.getHeader("Authorization"); if(authorization == null){ return null; } String token = authorization.replace("bearer ", ""); Claims claims = JwtUtils.getTokenBody(token); log.info("token claims info ==="+claims); if(claims == null){ return null; } //String userName = claims.get("user_name").toString(); return claims ; } }
public class JwtUtils { /** * . secret */ private static final String SECRET = "XXXXXXXXX"; /** * . 根据token解析Claims */ public static Claims getTokenBody(String token) { try{ return Jwts.parser() .setSigningKey(SECRET.getBytes("UTF-8")) .parseClaimsJws(token).getBody(); } catch (Exception ex){ return null; } } }