标签:导致 ant zone now() rate ret ring com oid
可以根据解析的结果得出token还有多久过期
package util; 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 Jws<Claims> parserToken(String token) { return Jwts.parser().setSigningKey(secret) .parseClaimsJws(token); } public static void main(String[] args) { // String s = generateToken(); String token = "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjAiLCJuYW1lIjoi5bCP6IuPIiwiZXhwIjoxNTkyMTI2NjcyfQ.9N4wQ-uuXNfBWJF1YKbeJIrF3x15MGXsqfwyMApIk9Q"; Jws<Claims> claimsJws = parserToken(token); System.out.println(claimsJws); } }
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); } }
标签:导致 ant zone now() rate ret ring com oid
原文地址:https://www.cnblogs.com/dongma/p/13125106.html