我试图创建 JWT(“JOT”)令牌以使我的 api 调用真实。当我尝试使用 RSA512 签名创建令牌时,我收到一条错误消息
java.lang.IllegalArgumentException:必须使用 RSA PrivateKey 计算 RSA 签名。指定的 javax.crypto.spec.SecretKeySpec 类型的密钥不是 RSA PrivateKey。
我正在使用以下代码:
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS512;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRET_KEY);
Key signingKey = new SecretKeySpec(apiKeySecretBytes,
signatureAlgorithm.getJcaName());
JwtBuilder builder = Jwts.builder().claim("uuid",
id).setIssuedAt(now).setExpiration(new Date(600000))
.signWith(signatureAlgorithm, signingKey);
Run Code Online (Sandbox Code Playgroud)
注意:我的“SECRET_KEY”是一个字符串,它是一个在线随机生成的私钥。我的问题是如何从使用 RSA 密钥大小编码为 4096. 4096 的字符串中获取 Key 对象,因为我使用的是 RSA512 加密,建议对 RSA512 使用 4096 密钥