I want to fetch the roles stored as claim in JWT. I am using following code to do that:
DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC512(SecurityConstants.SECRET.getBytes()))
.build()
.verify(token.replace(SecurityConstants.TOKEN_PREFIX, ""));
String user = decodedJWT.getSubject();
Claim claims = decodedJWT.getClaims();
String roles = claims.get("roles").toString();
But this ends up giving object code:
(Value of roles)JSONNodeClaim@10091
I debugged the code and found claims like this:
How can i extract the "ROLE_ADMIN" ?
You can cast the claim to the Claim class and call the .asString() method to return it as a string:
String claims = ((Claim) decodedJWT.getClaim("roles")).asString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With