我有一个以下实用程序类,但每当我通过验证方法检查过期令牌时,它都不会抛出 JWtVerificationException
。
public class Token {
private static String SECRET = "c3bff416-993f-4760-9275-132b00256944";
public static String get(String name, String value) throws UnsupportedEncodingException {
return JWT.create()
.withIssuer("auth0")
.withClaim(name, value)
.withClaim("random", String.valueOf(UUID.randomUUID()))
.withExpiresAt(new Date(System.currentTimeMillis() + (4 * 60 * 60 * 1000)))
.sign(Algorithm.HMAC256(Token.SECRET));
}
public static DecodedJWT verify(String token) throws JWTVerificationException, UnsupportedEncodingException {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(Token.SECRET))
.withIssuer("auth0")
.acceptExpiresAt(4)
.build();
return verifier.verify(token);
}
}
根据网站 https://github.com/auth0/java-jwt
验证令牌时,时间验证会自动发生,导致
JWTVerificationException
在值无效时被抛出。
编辑:
客户端每 5 分钟更新一次令牌的情况下,是否会继续工作,或者我应该增加几秒钟以适应任何网络延迟?
创造
.withExpiresAt(new Date(System.currentTimeMillis() + (5 * 60 * 1000))) // 5 minutes
核实
.acceptExpiresAt(5 * 60) // accept expiry of 5 minutes
原文由 Developer 发布,翻译遵循 CC BY-SA 4.0 许可协议
JWT.create().withExpiresAt(new Date(System.currentTimeMillis() + (5 * 60 * 1000)))
表示您将创建一个令牌,该令牌将在 5 分钟后过期。好像不错JWT.require(xxx).acceptExpiresAt(5 * 60)
表示您将接受5分钟前 已经过期 的令牌。即使考虑到网络延迟,5分钟的余地仍然太长。它应该在几秒钟内。