0
public boolean validateToken(String token){
try {
Jws<Claims> claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token);
if (claims.getBody().getExpiration().before(new Date())) {
return false;
}
return true;
} catch (AuthenticationException | ExpiredJwtException e) {
throw new InvalidJwtAuthenticationException("token expirado ");
}
}
I have this method that valid the token of the requests, but it may happen that the token is invalid and this exception is released, but it is only released on the console and I get an HTML file in Postman where only indicates the Httpstatus 500. All other Exceptions are released correctly, only the one that is not. All other exceptions work like this:
{
"timestamp": "2021-03-05T15:58:46.596+00:00",
"message": "Esse email já está em uso",
"details": "uri=/auth/register"
}
The Exception I’m trying to launch:
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class InvalidJwtAuthenticationException extends AuthenticationException {
public InvalidJwtAuthenticationException(String exception) {
super(exception);
}
}
My custom Handler:
@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public final ResponseEntity<ExceptionResponse> handlerAllException(Exception ex, WebRequest request){
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(GenericErrorRegister.class)
public final ResponseEntity<ExceptionResponse> handlerGenericErrorRegister(Exception ex, WebRequest request){
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(UnsupportedMathOperationException.class)
public final ResponseEntity<ExceptionResponse> handlerBadRequestException(Exception ex, WebRequest request){
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(ResourceNotFoundException.class)
public final ResponseEntity<ExceptionResponse> handlerNotFoundException(Exception ex, WebRequest request){
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(InvalidJwtAuthenticationException.class)
public final ResponseEntity<ExceptionResponse> handlerInvalidJwtAuthenticationException(Exception ex, WebRequest request){
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(InvalidEmailToRegisterException.class)
public final ResponseEntity<ExceptionResponse> handlerInvalidEmailToRegisterException(Exception ex, WebRequest request){
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
}
}