Improve JwtHelper exception handling (#1712)

Catch specific exceptions and don't log errors but instead add an appropriate message and preserve the stacktrace.

Signed-off-by: Wouter Born <github@maindrain.net>
pull/1714/head
Wouter Born 2020-10-11 10:03:39 +02:00 committed by GitHub
parent 23e8f18e7f
commit ab1fa65aa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 6 deletions

View File

@ -34,6 +34,8 @@ import org.jose4j.jwk.RsaJwkGenerator;
import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.MalformedClaimException;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException;
@ -126,9 +128,8 @@ public class JwtHelper {
String jwt = jws.getCompactSerialization();
return jwt;
} catch (Exception e) {
logger.error("Error while writing JWT token", e);
throw new RuntimeException(e.getMessage());
} catch (JoseException e) {
throw new RuntimeException("Error while writing JWT token", e);
}
}
@ -151,9 +152,8 @@ public class JwtHelper {
List<String> roles = jwtClaims.getStringListClaimValue("role");
Authentication auth = new Authentication(username, roles.toArray(new String[roles.size()]));
return auth;
} catch (Exception e) {
logger.error("Error while processing JWT token", e);
throw new AuthenticationException(e.getMessage());
} catch (InvalidJwtException | MalformedClaimException e) {
throw new AuthenticationException("Error while processing JWT token", e);
}
}
}