JWT 工具
可以根据解析的结果得出token还有多久过期
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jws; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class JwtUtils { static String secret = "secret"; public static String generateToken() { Instant instant = LocalDateTime.now().plusHours(2).atZone(ZoneId.systemDefault()).toInstant(); return Jwts.builder() .claim("id", "0") .claim("name","小苏") .setExpiration(Date.from(instant)) .signWith(SignatureAlgorithm.HS256, secret) .compact(); } private static Claims parserToken(String token) { Claims claims = Jwts.parser().setSigningKey("") .parseClaimsJws(token) .getBody(); return claims; } public static void main(String[] args) { // String s = generateToken(); String token = "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjAiLCJuYW1lIjoi5bCP6IuPIiwiZXhwIjoxNTkyMTI2NjcyfQ.9N4wQ-uuXNfBWJF1YKbeJIrF3x15MGXsqfwyMApIk9Q"; Claims claims = parserToken(token); System.out.println(claims); } }
import java.util.Date; import java.util.concurrent.ExecutionException; public class Client { public static void main(String[] args) throws ExecutionException { // 这里要指定为long类型,否则int会发生溢出,导致计算不准确 Date date = new Date(1592126672 * 1000L); System.out.println(date); } }