package com.takensoft.common.exception;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author takensoft
 * @since 2025.01.22
 * @modification
 *     since    |    author    | description
 *  2025.01.22  |  takensoft   | 최초 등록
 *
 * AuthenticationEntryPoint - 인증되지 않은 사용자의 요청을 처리하는 인터페이스
 *
 * 인증되지 않은 사용자의 요청을 처리하는 클래스
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

    /**
     * @param req - HTTP 요청 객체
     * @param res - HTTP 응답 객체
     * @param ae - 인증 예외
     * @throws IOException - 입출력 예외 발생 시
     * @throws ServletException - 서블릿 예외 발생 시
     *
     * 인증되지 않은 사용자가 요청을 보냈을 때 실행
     */
    @Override
    public void commence(HttpServletRequest req, HttpServletResponse res, AuthenticationException ae) throws IOException, ServletException {
        log.info("Requester IP: {}", req.getRemoteAddr()); // 요청자의 IP 주소 로그 출력
        log.info("Request URI: {}", req.getRequestURI()); // 요청 URI 로그 출력
        FilterExceptionHandler.authenticationEntryPoint(res, ae);
    }
}
