Search code examples
spring-bootjwtaccess-token

How to parse a JWT token to get the actual values from it


I am using java-jwt plugin to generate jwt token from my spring boot services and i am succesfully able to do that.

But I have a situation where I am adding some user specific information in the token and while validating it I am trying to get that information by decoding the token and building the verifier to validate the token. The way I am trying to get this information is by getting the claim first and from this claim i need to get the actual value that I added for some of the claims by providing their respective keys.

Lets suppose I added username as string but when i decode the token and from the claim I try to get it using claim.get("UsernameKey").toString() I get some jsonNode reference instead of the actual value.

Did I am missing something or I need to follow some other process to get the actual values that i used when I generated this token kindly let me know as am struggling to get it fixed and will be really helpful.


Solution

  • You use the wrong method. Use asString() and not toString().

    You should use claim.get("UsernameKey").asString()