content: package org.goormthon.seasonthon.nocheongmaru.global.security.oauth.handler;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import jakarta.servlet.ServletException;
  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.goormthon.seasonthon.nocheongmaru.global.exception.common.ErrorResponse;
  
  import java.io.IOException;
  
  import static java.nio.charset.StandardCharsets.UTF_8;
  import static org.springframework.http.HttpStatus.UNAUTHORIZED;
  import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
  
  @RequiredArgsConstructor
  @Component
  public class Oauth2FailureHandler extends SimpleUrlAuthenticationFailureHandler {
      
      private final ObjectMapper objectMapper;
      
      @Override
      public void onAuthenticationFailure(
          HttpServletRequest request,
          HttpServletResponse response,
          AuthenticationException exception
      ) throws IOException {
          ErrorResponse errorResponse = ErrorResponse.builder()
              .status(UNAUTHORIZED.value())
              .message(exception.getMessage())
              .build();
          
          response.setStatus(UNAUTHORIZED.value());
          response.setContentType(APPLICATION_JSON_VALUE);
          response.setCharacterEncoding(UTF_8.name());
          
          String jsonResponse = objectMapper.writeValueAsString(errorResponse);
          response.getWriter().write(jsonResponse);
          
      }
      
  }
  