最初,我没有将Base64编码的RSAkey再次编码为base64,这是我收到此错误的原因。
java.lang.IllegalArgumentException:必须使用RSA私钥计算RSA签名。类型为javax.crypto.spec.SecretKeySpec的指定密钥不是RSA私钥。
每当我提供RSARSA 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证书加载私钥。