1 回答

TA貢獻1877條經驗 獲得超1個贊
最初我沒有提供 Base64 編碼的 RSAkey ,我將它再次編碼為 base64 ,這就是我收到此錯誤的原因。
java.lang.IllegalArgumentException:必須使用 RSA 私鑰計算 RSA 簽名。類型為 javax.crypto.spec.SecretKeySpec 的指定密鑰不是 RSA 私鑰。
每當我提供 RSAKey base 64 編碼或字節碼私鑰時,我都會回到錯誤之下
只能為 HMAC 簽名指定 Base64 編碼的密鑰字節。如果使用 RSA 或橢圓曲線,請改用 signWith(SignatureAlgorithm, Key) 方法。
當我提供私鑰的字符串/字節時,它一直在檢查 HMAC 算法。請參閱 JWTBuilder 中的以下代碼。
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, byte[] secretKey) {
Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
Assert.notEmpty(secretKey, "secret key byte array cannot be null or empty.");
Assert.isTrue(alg.isHmac(), "Key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
this.algorithm = alg;
this.keyBytes = secretKey;
return this;
}
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) {
Assert.hasText(base64EncodedSecretKey, "base64-encoded secret key cannot be null or empty.");
Assert.isTrue(alg.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
byte[] bytes = TextCodec.BASE64.decode(base64EncodedSecretKey);
return signWith(alg, bytes);
}
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, Key key) {
Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
Assert.notNull(key, "Key argument cannot be null.");
this.algorithm = alg;
this.key = key;
return this;
}
提供 java.security.key 類型的私鑰并且必須是 RSA 密鑰總是最好的主意。我使用 P12 證書加載私鑰。
添加回答
舉報