Skip to content

Instantly share code, notes, and snippets.

@rakshitshah94
Created February 24, 2021 11:28
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 rakshitshah94/385a06979e8900379f1c5faa9e7f660d to your computer and use it in GitHub Desktop.
Save rakshitshah94/385a06979e8900379f1c5faa9e7f660d to your computer and use it in GitHub Desktop.
Easy guide and code snippet to verify the generated JWT tokens -Beingcoders
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