【发布时间】:2021-06-17 11:35:02
【问题描述】:
现在我正在使用此代码生成 JWT 令牌并在我的项目中设置过期时间:
SecretKey secretKey = new SecretKeySpec(jwtSignKey.getBytes(), SignatureAlgorithm.HS256.getJcaName());
JwtPayload jwtPayload = new JwtPayload();
jwtPayload.setUserId(user.getId());
jwtPayload.setDeviceId(request.getDeviceId());
jwtPayload.setAppId(Long.valueOf(request.getApp().getKey()));
byte[] bytesEncoded = Base64.encodeBase64(JSON.toJSONString(jwtPayload).getBytes());
String accessToken = Jwts.builder().setPayload(new String(bytesEncoded))
.setExpiration(new Date(System.currentTimeMillis() + 30*1000))
.signWith(secretKey).compact();
但它告诉我:
Both 'payload' and 'claims' cannot both be specified. Choose either one.
那么我应该怎么做才能使用令牌设置过期时间?
【问题讨论】: