Convert a JWT expiration date to a cookie max age

JSON Web Token expiration times - a JWT exp - are sent in UTC as a time since the UNIX epoc.

Problem

You need to set a cookie max age to match the JWT's expiration time.

Solution

Use Java 8's new time library to generate a duration in seconds.

private static int createCookieMaxAge(int tokenExpires) {  
  return (int) Duration.between(
    Instant.now(),
    Instant.ofEpochSecond(tokenExpires)
    ).getSeconds();
}
comments powered by Disqus

Software Engineer living in NYC
More Detail →
  • Software Engineering
  • Full Stack
  • Java
  • Spring
  • Linux
  • MongoDB
  • Objective-C
← Back

Recent Posts