#Spring security exception handling

9 messages · Page 1 of 1 (latest)

south phoenix
#

Hello does anyone how to handle jwt token exception? so far i can catch only spring security exception but not jwt exception like expired jwt token or signature exception.

candid jayBOT
#

This post has been reserved for your question.

Hey @south phoenix! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

proven wedge
#

catching specific JWT exceptions: ```java
try {
// Code that parses/validates JWT
} catch (ExpiredJwtException e) {
// Handle expired token
} catch (SignatureException e) {
// Handle invalid signature
} catch (MalformedJwtException e) {
// Handle invalid token
}

south phoenix
#
 try {
                    username = jwtService.extractUsername(token);
                } catch (ExpiredJwtException e) {
                    throw new BadCredentialsException("Expired jwt token", e);
                
                } catch (SignatureException e) {
                    throw new BadCredentialsException("Corrupted jwt token signature", e);
                } catch (MalformedJwtException e) {
                    throw new BadCredentialsException("Malformed jwt token", e);
                } catch (Exception e) {
                    throw new BadCredentialsException("Invalid jwt token", e);
                }
#

i have written like this

#

but it throws exception in the console and a long exception with the message is thrown on client

#

i want to throw response entity object and return message