In this post under JJWT I will show with example how to check whether a JWT is secured or not.
JwtParser class has a “isSigned” method which will figure out whether the JWT is secured or not. All we need is to pass the test JWT string as an argument to this method.
This method returns true or false, true means JWT is secured or else false.
Below is the complete code
Main class
1 package defaultPackage;
2 import io.jsonwebtoken.JwtParser;
3 import io.jsonwebtoken.JwtParserBuilder;
4 import io.jsonwebtoken.Jwts;
5
6 public class Example3 {
7 public static void main(String[] args) {
8 JwtParserBuilder jwtParserBuilder = Jwts.parser();
9 JwtParser jwtParser = jwtParserBuilder.build();
10 boolean result = jwtParser.isSigned("eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNjYxMDczNDU5LCJuYWVtIjoiU3VtYW50aCJ9.");
11 System.out.println(result);
12
13 result = jwtParser.isSigned("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c");
14 System.out.println(result);
15 }
16 }
In the above code, at line 8, I create an instance of “JwtParserBuilder” class by calling static “parser” method available on “Jwts” class.
At line 9, I create an instance of “JwtParser” class by calling “build” method on “jwtParserBuilder” instance.
At line 10 and 13, I check whether the given JWT is secured or not by calling “isSigned” method available on “jwtParser” instance and passing the JWT as an argument to the method.
In this way we can check whether a JWT is secured or not.