Auth0 Home Blog Docs

Java- intercept not valid token exception

java
spring-boot
spring-security

#1

I use Spring Security Authorization using this example
https://auth0.com/docs/quickstart/backend/java-spring-security/01-authorization
Unfortunaltelly i would like to intercept the error at the bootom of question in my Global Spring Interceptor:
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler implements ErrorController {

@ExceptionHandler( RuntimeException.class)
public ResponseEntity handleTokenExpiredException( RuntimeException ex, WebRequest request) {
loggerExceptionHandler.error("<<<>>>\n");
loggerExceptionHandler.error("MESSAGE: "+ex.getMessage());
HttpStatus status = HttpStatus.BAD_REQUEST;
HttpHeaders headers = new HttpHeaders();
headers.add(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
return handleExceptionInternal(ex, new ErrorTemplate(status, status.value(),
“Token expired ss”,
ex.getMessage(),
new Timestamp(System.currentTimeMillis())),
headers, status, request);
}
}
I create similar methods to other Exceptions like TokenExpiredException, AuthenticationException but still i cannot intercept this Exception produced by Authorization Provider - class JwtAuthenticationProvider .

EXCEPTION:

org.springframework.security.authentication.BadCredentialsException:
Not a valid token at
com.auth0.spring.security.api.JwtAuthenticationProvider.authenticate(JwtAuthenticationProvider.java:59)
(…) Caused by:
com.auth0.jwt.exceptions.TokenExpiredException:
The Token has expired on Sun Sep 10
17:44:16 CEST 2017. at
com.auth0.jwt.JWTVerifier.assertDateIsFuture(JWTVerifier.java:441)
at
com.auth0.jwt.JWTVerifier.assertValidDateClaim(JWTVerifier.java:432)
at
com.auth0.jwt.JWTVerifier.verifyClaims(JWTVerifier.java:373)
at
com.auth0.jwt.JWTVerifier.verify(JWTVerifier.java:355)
at
com.auth0.spring.security.api.authentication.AuthenticationJsonWebToken.(AuthenticationJsonWebToken.java:21)


#2