Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue while parsing JWT for claims

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:

enter image description here

How can i extract the "ROLE_ADMIN" ?

like image 472
Ujjwal Mishra Avatar asked Oct 15 '25 15:10

Ujjwal Mishra


1 Answers

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();
like image 188
Marko Previsic Avatar answered Oct 17 '25 05:10

Marko Previsic