jsonunmarshallingjose4j

JOSE4J: How to create a RsaJsonWebKey object from a JSON representation of RsaJsonWebKey


I want to save with JOSE4J the JSON representation of a RsaJsonWebKey object in JSON format and then recreate a RsaJsonWebKey object from it again. I have the marshalling part:

RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);

String jwkjson = rsaJsonWebKey.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);

but how to unmarshall it and recreate RsaJsonWebKey. That is where I'm stuck as I don't see a constructor of RsaJsonWebKey allowing that.

The question is related to this question

JWT becomes invalid after restarting the server

but it doesn't explain how to unmarshall.


Solution

  • The bottom half of the example in JWT becomes invalid after restarting the server does show how to get to a JsonWebKey/PublicJsonWebKey object from a JSON string. Using PublicJsonWebKey publicJsonWebKey = PublicJsonWebKey.Factory.newPublicJwk(jwkJson); will do the parsing/unmarshalling and can be cast to RsaJsonWebKey if need be.