Skip to content

Instantly share code, notes, and snippets.

@kundan59
Created October 3, 2019 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kundan59/3d83902224f74efd3206fc00e2b25933 to your computer and use it in GitHub Desktop.
Save kundan59/3d83902224f74efd3206fc00e2b25933 to your computer and use it in GitHub Desktop.
Jwt generation example
public String generateToken(final UUID sessionId) {
try {
final Algorithm algorithm = Algorithm.HMAC256(JWT_TOKEN_KEY);
final String id = String.valueOf(sessionId);
final String token;
return JWT.create()
.withIssuedAt(Date.from(ZonedDateTime.now().toInstant()))
.withExpiresAt(Date.from(ZonedDateTime.now().plusMinutes(EXPIRATION_TIME).toInstant()))
.withClaim("userId", id)
.withIssuer("Admin")
.sign(algorithm);
} catch (JWTCreationException e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException("Token not generated");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment