Parsing simple unsigned JWT containing plaintext as payload

In this post under JJWT, I will show with example how to parse unsigned JWT containing plaintext as payload instead of serialized Claims object.

As shown in previous post under JJWT, whenever we need to parse a JWT string. We need to create an instance of “JwtParser” as shown below


JwtParserBuilder jwtParserBuilder = Jwts.parserBuilder();
JwtParser jwtParser = jwtParserBuilder.build();

As shown in the above code, first we create an instance of “JwtParserBuilder” from “Jwts” class by calling its static method “parserBuilder”.

Next we create an instance of “JwtParser” by calling “build” method on “jwtParserBuilder” instance.

In my previous posts I showed how to parse a unsigned JWT containing claims data by calling “parseClaimsJwt” method on “JwtParser” instance.

Similar to “parseClaimsJwt” there is a corresponding instance method by name “parsePlaintextJwt” in “JwtParser” instance.

We use this method to parse unsigned JWT containing plaintext.

Below is the complete code for your reference

Main Class


1  package defaultPackage;
2  import io.jsonwebtoken.Header;
3  import io.jsonwebtoken.Jwt;
4  import io.jsonwebtoken.JwtParser;
5  import io.jsonwebtoken.JwtParserBuilder;
6  import io.jsonwebtoken.Jwts;
7  
8  public class Example6 {
9      public static void main(String[] args) {
10         JwtParserBuilder jwtParserBuilder = Jwts.parserBuilder();
11         JwtParser jwtParser = jwtParserBuilder.build();
12         Jwt<Header, String> jwt = jwtParser.parsePlaintextJwt("eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.SGkgbXkgbmFtZSBpcyBTdW1hbnRo.");
13         System.out.println(jwt.getHeader());
14         System.out.println(jwt.getBody());  
15     }
16 }

As shown in the above code, at line 12, we are calling “parsePlaintextJwt” on “jwtParser” instance and pass the JWT string.

At line 13 and 14, we get the JWT details like header and body.

In this way we have to parse a unsigned JWT containing plaintext as payload.

Leave a Reply