Obtain claim as POJO
See original GitHub issueCurrently DefaultClaims.get(String, Class)
does a bit of date foo or casts the value removed from the claim map. It would be great if the claims object retained the original JSON (perhaps an internal Map<String, JsonNode>
) and used jackson to deserialize it into the passed class. This might be useful when a claim value is an object, or alternative json types (like BigDecimal
), or to allow extensions into things jjwt doesn’t support (like java 8 Optional
).
Does this seem like a valid feature candidate?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How do I get a custom field out of the payload using JJWT
We can use Jackson's object mapper to convert Claims (which is a Map<String, Object> ) to our custom claim java object.
Read more >Claims (JJWT :: API 0.11.2 API) - Javadoc.io
It is easiest to create a Claims instance by calling one of the JWTs.claims() factory methods. ... A JWT obtained after this timestamp...
Read more >POJOs - jOOQ
Fetching data in records is fine as long as your application is not really layered, or as long as you're still writing code...
Read more >JOSE object / JSON Web Token (JWT) parsing - Connect2id
Parsing JOSE objects or JWTs of a certain expected type (plain, signed or encrypted) is easy. If your application accepts more than one...
Read more >Implementing hierarchical relationships with Corda ... - Medium
VehicleDetail POJO class ... Claim POJO class. Step2: Create the Claim ... A button that says 'Get it on, Google Play', and if...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So I’d propose adding another method,
JsonNode getJson(String)
to retrieve the original JSON value of a claim field. This way all manner of mapping can be covered by the user without adding to jjwt.The existing
get(String, Class)
method could be modified to look something like thisThis could be implemented by adding a field
Map<String, JsonNode> json
toJwtMap
orDefaultClaims
.getJson(String)
would then simply return values fromjson
.This addition to
get(String, Class)
doesn’t change any existing use cases, but gives one more chance to return without exception.getJson(String)
then covers the rest of cases where the user has complex mapping and needs to use their own mapper.While serializing an object to a string and putting that in the claim is easily supported most places, not all JWTs a user could receive will be issued by them. For example, OpenID Connect specifies
address
as a standard claim, which is an object. If I wanted to build an OpenID Connect library on jjwt, I’d probably want to turn that into anAddressClaim
pojo (of course I could get it as a map and map that map to my pojo, but I think most would rather just work with the json).What do you think?
Closing this as a duplicate of #369.