如何使用jose.4.j输出JWK设置端点?

时间:2016-11-01 20:10:56

标签: java jose4j jwk

我可以看到如何使用jose.4.j创建新的Key,但我想知道的是如何获取它们的数组并输出可公开访问的JSON列表以用作OiDC中的发现端点。像IETF draft example ...

这样的东西
 {"keys":
   [
     {"kty":"EC",
      "crv":"P-256",
      "x":"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
      "y":"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
      "use":"enc",
      "kid":"1"},

     {"kty":"RSA",
      "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx
 4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMs
 tn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2
 QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbI
 SD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqb
 w0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
      "e":"AQAB",
      "alg":"RS256",
      "kid":"2011-04-29"}
   ]
 }

我想象有一些显而易见的东西我会像toString()那样没有显示私钥吗?

1 个答案:

答案 0 :(得分:2)

String toJson(JsonWebKey.OutputControlLevel outputControlLevel)上的JsonWebKeySet方法仅在JsonWebKey.OutputControlLevel.PUBLIC_ONLY调用时才包含公开部分。这应该会给你你想要的东西。 JsonWebKeySet可以使用JsonWebKeys列表进行实例化。

像这样的东西,例如,

    RsaJsonWebKey rsaJwk1 = RsaJwkGenerator.generateJwk(2048);
    RsaJsonWebKey rsaJwk2 = RsaJwkGenerator.generateJwk(2048);
    EllipticCurveJsonWebKey ecJwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);

    JsonWebKeySet jwks = new JsonWebKeySet(rsaJwk1, rsaJwk2, ecJwk);

    String jwksJson = jwks.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY);

    System.out.println(jwksJson);

会给,

{"keys":[{"kty":"RSA","n":"4cejS2CzkJuoLe9ry1JDdS_-RkpR2UMLialeH9WleuQFbFT3caK-GtNwObcd9_xEZ2H7um2GgZuscCMepo7d0FmwQUnfnTy9-_ckdgvVCH6lF62H3Jd4FBWAlcp_100kEJUe9YwfytdJ0zpOLo5Q38kH31cyMZmZRfexf33CzMNhlgE3mydpM9NAOAnR8IXW43tl1lPEy_dvLE9kM4idIjhd5Xd0a_YmRr05PK4UgFot0cuzZgu0HHe1qM7aFevwSOQ_Y86g4ofjZWHQWGJRN6WCBlGLscUwB52-hlJC0GrVEI0Cb55bfAfu01P1eFTjwyjfwggyLFZLdiAkZl2Jkw","e":"AQAB"},{"kty":"RSA","n":"jyOndkwyHBaHMn1xTIPfKV8PulJi6lcqqnw3sOabE-hClQr6De_OEcMnfCHPuogz1oep0A7-SvjY6Lclp0Pa5JDnHy6fI8UkSmASS6cwEUVWg18taFazRu2GtKDmMzT0Sh7ZCRGLWU74p5hRHGCerVnLcJUqSzT7kuGViZWVaIt00SfI6OImDpJTnaoxSwasdrhIZ_j5ARM93B2RKFsEb1kZ_gPnlP_0Qzs62LLG-78QQzlfMTd6v29vNr1FPPeZe744ni5Rpp19vEfEShGSV6Vz8K_Z2JlYC8PA_cRx47FLvmj9fB1u_wWYNP5ryGjv2E5Vjy9tqnIOmmtByoTdFw","e":"AQAB"},{"kty":"EC","x":"EEzQrpMmJ4XQgJFNGFNVI2WLmoaIpMYBTNF1_D5lp2U","y":"rENpzGhayZRoVR-7cFWJKElDQdJkJrKrDJ0-FqNpstA","crv":"P-256"}]}