question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to get the Map from claims during parsing?

See original GitHub issue

I have set-up custom claims as map during jwt generation. when decoding jwt, I want to retrieve the map.

I am trying to do this;

 Jws<Claims> jwsClaims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token);
 Claims claims = jwsClaims.getBody();

how to get Map<String,Object> from claims?

This is how I set custom claims

Jwts.builder()
                .setClaims(getMapFromResponseObject(myObj))
                .setIssuedAt(now)
                .setExpiration(expiration)
                .setIssuer(ISSUER)
                .signWith(SignatureAlgorithm.HS512, secretKey)
                .compact();


private Map<String,Object> getMapFromResponseObject(MyObject myObj){
        ObjectMapper m = new ObjectMapper();
        Map<String,Object> props = m.convertValue(myObj, Map.class);
        return props;
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
eric-taixcommented, Aug 28, 2016

As @dogeared said just use JwtBuilder#claim method :

Map<String,Person> serializedObjects = myMapper.serialize(myObjectsToConvert);  
Jwts.builder()  
                .claim("myPrivateClaimKey", serializedObjects)  
                .setIssuedAt(now)  
                .setExpiration(expiration)  
                .setIssuer(ISSUER)  
                .signWith(SignatureAlgorithm.HS512, secretKey)  
                .compact();

The JSON representation will be:

{
  "iss": "ISSUER"
  ...
  "myPrivateClaimKey": {
    "hello": {
      "name": "xxxx"
    },
    "world": {
      "name": "yyyy"
    }
  }
}

When you deserialize the Jwt body, just get “myPrivateClaimKey”:

Object serializedObjects = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody().get("myPrivateClaimKey");  
Object objects = myMapper.deserialize(serializedObjects);

Does it make sense?

1reaction
sridhar1982commented, Aug 26, 2016

Just as I cited above.

Map<String,Person> map = new HasMap<>();
map.put("hello", new Person("hello"));
map.put("world", new Person("world");

Jwts.builder()
                .setClaims(map)
                .setIssuedAt(now)
                .setExpiration(expiration)
                .setIssuer(ISSUER)
                .signWith(SignatureAlgorithm.HS512, secretKey)
                .compact();

now when I parse, I would like to get back the map which i set as claims i.e Map<String,String> map = (Map<String,String>) claims;

The claims by itself contains expiration, iss, subject etc which I do not want. Just the map which I set as claims. The reason I ask this, I have a mapper which coverts this map to java object and vice-versa.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert from io.jsonwebtoken.Claims to java.util.Map ...
I am able to create a JWT token with custom payloads successfully. Now while i am going to parse it, I need Map<String,...
Read more >
Go Notes: Parsing Claims in Firebase JWT Token
I struggled to parse the Firebase JWT Claims. Because claims may contain strings, maps, and arrays, the claims field type is an []interface{}....
Read more >
Spring Security – Map Authorities from JWT | Baeldung
In this tutorial, we'll show how to customize the mapping from JWT (JSON Web Token) claims into Spring Security's Authorities.
Read more >
Parsing and producing JSON - Apache Groovy
It parses a JSON String and recursively converts it to a list or map of objects. The other parse* methods are similar in...
Read more >
JOSE object / JSON Web Token (JWT) parsing - Connect2id
Parsing JOSE objects or JWTs of a certain expected type (plain, ... e) { // Invalid plain JWT encoding } // continue with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found