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

时间:2020-10-12 14:11:17

标签: java nimbus-jose-jwt

我有这样的代码:

        // 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."
    }
  ]
}

我应该怎么做才能产生这种格式而不是以前的格式?

1 个答案:

答案 0 :(得分:0)

我不好,我的端点错了。

代替:

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

应该是:

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