I am using jjwt
for jwt token creation. Everything works fine when setting expiration date with local system time, i.e.
Date expDate = new Date(new Date().getTime() + 180000); //java.util.Date
But I tried using UTC format date time and signed the jwt token with same 3 min expiry date. And now it is throwing ExpiredJwtException
though even i am validating as soon as creating the token. I am using SimpleDateFormat for setting timezone to utc.
This is my code for creating token using jjwt in java:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date expDate, issDate;
try {
expDate = (Date) simpleDateFormat.parse(sdf.format(new Date().getTime() + 180000));
issDate = (Date) simpleDateFormat.parse(sdf.format(new Date().getTime()));
JwtBuilder builder = Jwts.builder()
.setExpiration(expDate)
.setIssuedAt(issDate)
.setId(id)
.signWith(signingKey, signatureAlgorithm);
jwtToken = builder.compact();
} catch (ParseException ex) {
}
The token gets successfully created. I can verify the contents online as well. expDate is 3 min ahead of issDate. I am also calling method for verifying the token as soon as after it was created by passing that created token. My verification method has:
try {
Jwts.parser().setSigningKey(signingKey).parseClaimsJws(token);
log.info("jwt verification success");
} catch (ExpiredJwtException exJwt) {
log.info("expired jwt : \n{}", exJwt.getMessage());
} catch (JwtException e) {
log.info("tampered jwt");
}
But I am getting ExpiredJwtException
. The error is
expired jwt : JWT expired at 2019-05-17T01:24:48Z. Current time: 2019-05-17T07:06:48Z, a difference of 20520836 milliseconds. Allowed clock skew: 0 milliseconds.
From my log, the issued date and expiration date in my token at this time is:
issued date is: 2019-05-17T07:06:48.000+0545
expiry date is: 2019-05-17T07:09:48.000+0545
How is this happening? And thank you for you help.