如何配置 RSAKey.Builder 以产生没有空值的简单结果

How to configure RSAKey.Builder to produce simple result without null values

我有这样的代码:

        // get keyPair...
        final JWK jwk = new RSAKey.Builder((RSAPublicKey) keyPair.getPublic())
            .privateKey((RSAPrivateKey) keyPair.getPrivate())
            .keyUse(KeyUse.SIGNATURE)
            .algorithm(JWSAlgorithm.RS256)
            .keyID("1")
            .build();

    return new JWKSet(jwk.toPublicJWK());

它产生的结果是这样的:

keys: [
{
    keyStore: null,
    private: false,
    modulus: { },
    privateExponent: null,
    publicExponent: { },
    secondPrimeFactor: null,
    firstPrimeFactor: null,
    secondFactorCRTExponent: null,
    firstCRTCoefficient: null,
    firstFactorCRTExponent: null,
    requiredParams: {
        e: "AQAB",
        kty: "RSA",
        n: "123456789-987654321...etc."
    },
    otherPrimes: [ ],
    algorithm: {
        name: "RS256",
        requirement: null
    },
    keyType: {
        value: "RSA",
        requirement: "REQUIRED"
    },
    x509CertThumbprint: null,
    x509CertSHA256Thumbprint: null,
    parsedX509CertChain: null,
    keyID: "1",
    keyUse: {
        value: "sig"
    },
    keyOperations: null,
    x509CertURL: null,
    x509CertChain: null
    }
    ],
    additionalMembers: { }
}

我希望有更简单的东西,像这样:

{
  "keys": [
    {
      "kty": "RSA",
      "e": "AQAB",
      "use": "sig",
      "kid": "65b413b1-94c8-48ce-a79a-ba4978460205",
      "alg": "RS256",
      "n": "123456789-987654321...etc."
    }
  ]
}

我应该怎么做才能生成这种格式而不是前一种格式?

不好意思,我的端点错了。

而不是:

@GetMapping
public ResponseEntity<JWKSet> getJwks() {
    return ResponseEntity
            .status(HttpStatus.OK)
            .body(jwksService.getJwkSet());
}

假设是:

@GetMapping
public Map<String, Object> getJwks() {
    return jwksService.getJwkSet().toJSONObject(true);
}