gpt4 book ai didi

java - JWT 中的类转换异常

转载 作者:行者123 更新时间:2023-11-30 06:44:54 25 4
gpt4 key购买 nike

我正在使用 JwtBuilder 创建一个 token 字符串。但是在使用键提取值时,它会给出 ClassCastException。为了更好地理解代码片段,下面提供了:

代币创建:

private JwtBuilder getJwtBuilder(
String jti,
Long issuedAt,
Long expiredAt,
Long businessAccountId,
Long consumerAccountId,
String deviceId
) {
JwtBuilder builder = Jwts.builder();
builder.setIssuer("SO");
builder.setSubject(TokenConstant.TOKEN_SUBJECT);
builder.setId(jti);
builder.setIssuedAt(new Date(issuedAt));
builder.setExpiration(new Date(expiredAt));
builder.claim(TokenConstant.BUSINESS_ACCOUNT_ID, businessAccountId);
builder.claim(TokenConstant.DEVICE_ID, deviceId);
builder.signWith(SignatureAlgorithm.HS512, secretKey);
return builder;
}

解码 token :

private JsonWebToken decodeToken(String jsonWebToken) {
try {
Jws<Claims> map = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(jsonWebToken);
Claims claims = map.getBody();
return getJsonWebTokenFromClaims(claims);
} catch (SignatureException | MalformedJwtException e) {
throw new InvalidTokenException(e.getMessage());
}
}

private JsonWebToken getJsonWebTokenFromClaims(Claims claims) {
JsonWebToken token = new JsonWebToken();
token.jti = claims.getId();
token.expirationTime = claims.getExpiration().getTime();
token.issuedAt = claims.getIssuedAt().getTime();
token.deviceId = (String) claims.get(TokenConstant.DEVICE_ID);
token.businessAccountId = (Long) claims.get(TokenConstant.BUSINESS_ACCOUNT_ID);
return token;
}

异常:

2018-04-23 10:27:04.476 ERROR b.c.i.s.c.MyExceptionHandler - Application error: {} java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

jwt版本:

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>

如您所见,我的代码在将 Integer 类型值转换为 Long 类型时出错。我无法理解,为什么 Object 类型隐式转换为 Integer

最佳答案

我建议将您的 jwt 依赖项的版本升级到版本 0.9.0

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>

此问题在此处报告为 an issue in Github这表示根据设置为声明的较小值自动绑定(bind)到数据类型。我正在复制问题中的确切声明以详细说明。

  String claimName = "long"
Long claimVal = new Long(5)
String compact = Jwts.builder().setClaims([(claimName): claimVal]).compact();
Claims claims = Jwts.parser().parse(compact).body as Claims
// `claims.get` throws `RequiredTypeException`
assert claims.get(claimName, Long) == claimVal

Here, Jackson finds that a 5 is small enough to fit in the Integer and so unmarshalls into it, instead of expected Long.

What makes matters worse is that when a value is large enough (≥ 2^31), Jackson will switch to Long and so code that naively always get()-s Integer will stop working:

但是,此问题已在 this pull request 中修复并且在更新版本的 jwt 库中可用。

更新库后,您可以使用以下代码在没有 RequiredTypeException 的情况下获得正确的值。

// businessAccountId is declared as Long I guess
token.businessAccountId = claims.get(TokenConstant.BUSINESS_ACCOUNT_ID, Long.class);

希望能解决您的问题。

关于java - JWT 中的类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49964955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com