使用 RS256 和 Jwts.builder().signWith() 生成令牌生成无效签名

Generate token with RS256 and Jwts.builder().signWith() produces invalid signature

使用 JWT Java library and producing a token with the RS256 algorithm, I always get an invalid signature with the jwt.io 调试器。这是我的示例代码,我试图让它尽可能简单地开始我当前的项目:

    // Create a legitimate RSA public and private key pair:
    KeyPair kp = RsaProvider.generateKeyPair();
    PublicKey publicKey = kp.getPublic();
    PrivateKey privateKey = kp.getPrivate();

    String jwt = Jwts.builder().setSubject("Joe").signWith(SignatureAlgorithm.RS256, privateKey).compact();

这段代码的灵感来源于测试class here.

知道我可能遗漏了什么吗?

jwt.io 调试器希望您提供与私钥关联的 public 密钥,该私钥用于签署以 Public 密钥文件 (PKCS#8) 格式编码的令牌。

确保您使用完全相同的格式指定它,示例如下:

-----BEGIN PUBLIC KEY-----
BASE64 DATA
-----END PUBLIC KEY-----