In this post under JJWT, I will explain with example, how to verify or assert the presence of standard claim in JWT token.
JJWT framework provides a list of methods through which
1) we can verify whether a standard claim exist in the JWT or not.
2) we can verify whether a standard claim’s value is equal to what we want.
The methods exist in JwtParserBuilder and are as shown below
JwtParserBuilder requireSubject(String subject)
JwtParserBuilder requireAudience(String audience)
JwtParserBuilder requireId(String id)
JwtParserBuilder requireIssuer(String issuer)
JwtParserBuilder requireIssuedAt(Date issuedAt)
JwtParserBuilder requireExpiration(Date expiration)
JwtParserBuilder requireNotBefore(Date notBefore)
The method names itself explains there purposes.
In our example, I will give a demo of “requireSubject” method.
Below is the complete main code for your reference
Main class
1 package defaultPackage;
2
3 import io.jsonwebtoken.Claims;
4 import io.jsonwebtoken.Header;
5 import io.jsonwebtoken.IncorrectClaimException;
6 import io.jsonwebtoken.Jwt;
7 import io.jsonwebtoken.JwtParser;
8 import io.jsonwebtoken.JwtParserBuilder;
9 import io.jsonwebtoken.Jwts;
10 import io.jsonwebtoken.MissingClaimException;
11
12 public class Example8 {
13 public static void main(String[] args) {
14 JwtParserBuilder jwtParserBuilder = Jwts.parserBuilder();
15 jwtParserBuilder = jwtParserBuilder.requireSubject("1234567890");
16 JwtParser jwtParser = jwtParserBuilder.build();
17 System.out.println("Parsing token 1");
18 Jwt<Header, Claims> jwt = jwtParser.parseClaimsJwt("eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNjYxNjU5OTU1LCJuYW1lIjoiU3VtYW50aCJ9.");
19 System.out.println(jwt.getHeader());
20 System.out.println(jwt.getBody());
21
22 try {
23 System.out.println("Parsing token 2");
24 jwt = jwtParser.parseClaimsJwt("eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJpYXQiOjE2Njg5MTkxMzksIm5hbWUiOiJTdW1hbnRoIn0.");
25 } catch (MissingClaimException exception) {
26 exception.printStackTrace();
27 }
28
29 try {
30 System.out.println("Parsing token 3");
31 jwtParserBuilder = jwtParserBuilder.requireSubject("123456789");
32 jwt = jwtParser.parseClaimsJwt("eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNjYxNjU5OTU1LCJuYW1lIjoiU3VtYW50aCJ9.");
33 } catch (IncorrectClaimException exception) {
34 exception.printStackTrace();
35 }
36 }
37 }
In the above code, at line 14 I create an instance of “JwtParserBuilder”.
At line 15, I am telling to the instance of “JwtParserBuilder” whichever JWT it parses, it has to assert that the JWT contains “subject” claim and its value matches with “1234567890”.
At line 18, we parse the first token and it will succeed without any error as the JWT contains “subject” claim and its value matches with user specified value.
At line 24, we parse the second token and it will fail because it doesn’t have “subject” claim. It throws “MissingClaimException” exception.
At line 32, we parse the third token and it will fail because it does have subject but the value we are expecting is not there, so it fails. It throws IncorrectClaimException exception.
Below is the output
Output
Parsing token 1
{typ=JWT, alg=none}
{sub=1234567890, iat=1661659955, name=Sumanth}
Parsing token 2
io.jsonwebtoken.MissingClaimException: Expected sub claim to be: 1234567890, but was not present in the JWT claims.
Parsing token 3
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.validateExpectedClaims(DefaultJwtParser.java:498)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:452)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:529)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.parseClaimsJwt(DefaultJwtParser.java:562)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.ImmutableJwtParser.parseClaimsJwt(ImmutableJwtParser.java:163)
at JJWTConcepts/defaultPackage.Example8.main(Example8.java:23)
io.jsonwebtoken.IncorrectClaimException: Expected sub claim to be: 123456789, but was: 1234567890.
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.validateExpectedClaims(DefaultJwtParser.java:505)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:452)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:529)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.DefaultJwtParser.parseClaimsJwt(DefaultJwtParser.java:562)
at jjwt.impl@0.11.5/io.jsonwebtoken.impl.ImmutableJwtParser.parseClaimsJwt(ImmutableJwtParser.java:163)
at JJWTConcepts/defaultPackage.Example8.main(Example8.java:31)