Created
October 3, 2019 06:10
-
-
Save kundan59/3d83902224f74efd3206fc00e2b25933 to your computer and use it in GitHub Desktop.
Jwt generation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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