class JWTLogoutHandler implements LogoutHandler {

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        Optional<String> token = authenticationService.getJwtTokenFromRequest(request);
        try {
            if (token.isPresent()) {
                invalidateJwtToken(failure, request, response, token.get());
            } else {
                failure.onAuthenticationFailure(request, response, new TokenNotProvidedException("The token you are trying to logout is not present in the header"));
            }
        } catch (ServletException e) {
            log.error("The response cannot be written during the logout exception handler: {}", e.getMessage());
        }
    }

}