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

1k views Asked by At

I have a code like this:

        // 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());

It produces result like this:

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: { }
}

I would expect something simpler, like this:

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

What should I do to produce this format instead of the former one?

1

There are 1 answers

0
JiKra On BEST ANSWER

My bad, my endpoint was wrong.

Instead of:

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

there is suppose to be:

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