java-jwt
java-jwt copied to clipboard
Fine-grained verification exception handling
I'm using this JWT library in my API authentication. I don't see many examples in the README. And I wanted to handle the verification exceptions in a fine-grained level. Based on the documented example, instead of just telling API consumers "Invalid Token", I wanted to verify the issuer and expiration time separately.
try {
final JWTVerifier verifier = new JWTVerifier("{{secret used for signing}}", "{{my-audience}}", "{{my-issuer}}");
final Map<String, Object> claims= verifier.verify(jwt);
} catch (JWTVerifyException e) {
// Invalid Token
}
Can anyone provide any examples? Thanks!
@yuanzhou You can use the type of exception.
- If it's an algorithm problem:
AlgorithmMismatchException - If it's a signature problem:
SignatureVerificationException - If it's the claims:
InvalidClaimException
Hi @yuanzhou. You should do as @ensonik suggested and check the type of the exception. This also works for v3 where JWTVerificationException is thrown but the cause is wrapped as follows:
SignatureVerificationException: the signature is invalidAlgorithmMismatchException: algorithm is not the specifiedInvalidClaimException: value is not the expected one