content: package com.anita.multipleauthapi.security.oauth2;
  
  import com.anita.multipleauthapi.service.util.CookieUtils;
  import jakarta.servlet.http.Cookie;
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletResponse;
  import lombok.RequiredArgsConstructor;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
  import org.springframework.stereotype.Component;
  import org.springframework.web.util.UriComponentsBuilder;
  
  import java.io.IOException;
  
  import static com.anita.multipleauthapi.security.oauth2.HttpCookieOAuth2AuthorizationRequestRepository.REDIRECT_URI_PARAM_COOKIE_NAME;
  
  @Component
  @RequiredArgsConstructor
  public class OAuth2AuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
  
      private final HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository;
  
      @Override
      public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
          String targetUrl = CookieUtils
                  .getCookie(request, REDIRECT_URI_PARAM_COOKIE_NAME)
                  .map(Cookie::getValue)
                  .orElse(("/"));
  
          targetUrl = UriComponentsBuilder.fromUriString(targetUrl)
                  .queryParam("error", exception.getLocalizedMessage())
                  .build().toUriString();
  
          httpCookieOAuth2AuthorizationRequestRepository.removeAuthorizationRequestCookies(request, response);
  
          getRedirectStrategy().sendRedirect(request, response, targetUrl);
      }
  
  }