如何配置RSAKey.Builder以生成简化结果,而不包含空值。

huangapple go评论67阅读模式
英文:

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

问题

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

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

得分: 0

抱歉,我的端点设置错误。

应该是:

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

My bad, my endpoint was wrong.

Instead of:

@GetMapping
public ResponseEntity&lt;JWKSet&gt; getJwks() {
    return ResponseEntity
            .status(HttpStatus.OK)
            .body(jwksService.getJwkSet());
}

there is suppose to be:

@GetMapping
public Map&lt;String, Object&gt; getJwks() {
    return jwksService.getJwkSet().toJSONObject(true);
}

huangapple
  • 本文由 发表于 2020年10月12日 22:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64319547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定