Issue
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" ?
Solution
You can use the chain method call to get the below:
String claims = decodedJWT.getClaim("roles").as(TextNode.class).asText();
Answered By - Soni
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)