title: How to check if a JWT Token has expired without throw exceptions?
body: <p>I'm developing a Java application (using Spring Boot) and I need some help:
  This application receives as input a <strong>JWT token</strong> which I process in a method. The method for now is the following:</p>
  <pre><code>private UsernamePasswordAuthenticationToken myMethod(HttpServletRequest request) {
      
      // Code that gets the 'token' String
  
      try{
      
      Map&lt;String, String&gt; registry = ((Map&lt;String, String&gt;) (token.getBody().get(&quot;registry&quot;)));
      String sub = ((String) (parsedToken.getBody().get(&quot;sub&quot;)));
  
  
      UsernamePasswordAuthenticationToken finalToken = new UsernamePasswordAuthenticationToken(sub, null, null);
                              
              
      return finalToken; 
  
              
      } catch (ExpiredJwtException exception) { // Only here I have the certainty that the token has expired!
              // Code that handles the exception
      }
  </code></pre>
  <p>}</p>
  <p>However, I need to implement a logic that must check in several places whether the token obtained has expired or not, <strong>without running this method every time</strong>. The only way I have to know if <code>token</code> has expired is the exception raised by <code>ExpiredJwtException</code>.</p>
  <p>Is there any way to know <strong>if the token has expired without going through the catched exception</strong>? For example, it would be very useful if there was a &quot;token&quot; class that has an <code>.isExpired</code> attribute, or something like that.</p>
  <p>I don't want to go into handling the exception because it means that I would always depend on the (long) code of the <code>try</code> block every time I need to check if a token has expired or not, and I don't want it to be.</p>