vaibhav
vaibhav

Reputation: 4103

JWT token expiry time gets trimmed in generated token

I am generating a JWT token in one of my application to communicate to some third party APIs, i could create the token properly, untill i started facing a strange issue below is how i am setting the expuration date for my token:

JWTClaimsSet jwtClaims = new JWTClaimsSet();
jwtClaims.setExpirationTime(new Date(new Date().getTime() + 1000*60*120));
jwtClaims.setCustomClaim("myobj", prepare(myobj));
JWSHeader header = new JWSHeader(JWSAlgorithm.RS256);
header.setContentType("text/plain");

and just below this i do a

System.out.println("after :::"+jwtClaims.getExpirationTime().getTime());

which prints the value as: 1420636467027 however when i decode my token i am getting this value as: 1420636467 so basically the last 3 digits are getting trimmed, moreover the third party API is failing my calls on account of expired token and rightly so. Is there anything additional which i need to do in order to avoid this situtation' Any help in this regard is highly appreciated.

Upvotes: 0

Views: 4176

Answers (2)

Abhilash
Abhilash

Reputation: 11

When adding time with milliseconds, use a long value. This should resolve it:

.expirationTime(new Date(new Date().getTime() + 720000L))

Upvotes: 1

user3904269
user3904269

Reputation: 91

All the times in JWT token like issued at time(iat), not before time(nbf) and expiration time(exp) are supposed to be measured in seconds since 1/1/1970. Since you are specifying time in milliseconds, that might be causing issues at the server end when the server cracks open your JWT token. Just start using time in seconds. Hopefully that shall resolve your problem.

Upvotes: 3

Related Questions