Created
February 24, 2021 11:28
-
-
Save rakshitshah94/385a06979e8900379f1c5faa9e7f660d to your computer and use it in GitHub Desktop.
Easy guide and code snippet to verify the generated JWT tokens -Beingcoders
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
import javax.xml.bind.DatatypeConverter; | |
import io.jsonwebtoken.Jwts; | |
import io.jsonwebtoken.Claims; | |
//Sample method to validate and read the JWT | |
private void verifyJWT(String jwt) { | |
//This line will throw an exception if it is not a signed JWS (as expected) | |
Claims claims = Jwts.parser() | |
.setSigningKey(DatatypeConverter.parseBase64Binary(apiKey.getSecret())) | |
.parseClaimsJws(jwt).getBody(); | |
System.out.println("ID: " + claims.getId()); | |
System.out.println("Subject: " + claims.getSubject()); | |
System.out.println("Issuer: " + claims.getIssuer()); | |
System.out.println("Expiration: " + claims.getExpiration()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment