title: How to extend expiration time java json web token?
body: <p>I try to create Json Web Token in Java with <a href="https://github.com/jwtk/jjwt/" rel="noreferrer">jjwt library</a></p>
  <p>But I have problem when I try to extend the expiration time.</p>
  <p>I try it by the code below.</p>
  <pre><code>public class Main {
  public static void main(String args[]) {
      byte[] key = new byte[64];
      new SecureRandom().nextBytes(key);
      Date date = new Date();
      long t = date.getTime();
      Date expirationTime = new Date(t + 5000l); // set 5 seconds
  
      String compact = Jwts.builder().setSubject(&quot;Joe&quot;).setExpiration(expirationTime).signWith(SignatureAlgorithm.HS256, key).compact();
      System.out.println(&quot;compact : &quot; + compact);
      try {
          String unpack = Jwts.parser().setSigningKey(key).parseClaimsJws(compact).getBody().getSubject();
          System.out.println(&quot;unpackage 0 : &quot; + unpack);
          
          // check if the expiration work.
          Thread.sleep(3000);
          System.out.println(&quot;unpackage 1 : &quot; + Jwts.parser().setSigningKey(key).parseClaimsJws(compact).getBody().getSubject());
          
          //extend the expration time.
          Date date1 = new Date();
          long t1 = date1.getTime();
          Date expirationTime1 = new Date(t1 + 5000l); //prolongation 5 seconds
          Jwts.parser().setSigningKey(key).parseClaimsJws(compact).getBody().setExpiration(expirationTime1).getSubject();
          
          // check if the extend expiration work.
          Thread.sleep(3000);            
          System.out.println(&quot;unpackage 2 : &quot; + Jwts.parser().setSigningKey(key).parseClaimsJws(compact).getBody().getSubject());
      } catch (InterruptedException | ExpiredJwtException ex) {
          System.out.println(&quot;exception : &quot; + ex.getMessage());
          Thread.currentThread().interrupt();
      }
  }
  </code></pre>
  <p>The result is :</p>
  <pre>
  compact : eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UiLCJleHAiOjE0Mjk2NjU1MjB9.oMY2mDHvNoMZqBfic41LbiKvAyi93wIfu_WgIADb9Wc
  unpackage 0 : Joe
  unpackage 1 : Joe
  exception : JWT expired at 2015-04-22T08:18:40+0700. Current time: 2015-04-22T08:18:42+0700
  </pre>
  <p>So it mean, the <em>unpackage2</em> cant run, Because it was expiration.</p>
  <p>I trying to extend the expiration time.</p>
  <p>Because I apply the code on web application.</p>
  <p>If user still connect with my application, He should not get token timeout.</p>
  <p>I have found <a href="https://stackoverflow.com/questions/26739167/jwt-json-web-token-automatic-prolongation-of-expiration">another question</a> like mine.</p>