diff --git a/src/main/java/com/splanet/splanet/core/exception/GlobalExceptionHandler.java b/src/main/java/com/splanet/splanet/core/exception/GlobalExceptionHandler.java index fe4c9820..4b0728e9 100644 --- a/src/main/java/com/splanet/splanet/core/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/splanet/splanet/core/exception/GlobalExceptionHandler.java @@ -1,17 +1,31 @@ package com.splanet.splanet.core.exception; import com.splanet.splanet.core.exception.ResponseConstants; +import com.splanet.splanet.log.service.LogService; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.ConstraintViolationException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import java.io.IOException; + @RestControllerAdvice public class GlobalExceptionHandler { + private final LogService logService; + + // LogService를 생성자 주입 받습니다. + public GlobalExceptionHandler(LogService logService) { + this.logService = logService; + } - @ExceptionHandler(BusinessException.class) + + @ExceptionHandler(BusinessException.class) protected ResponseEntity handleBusinessException(BusinessException ex) { String errorMessage = ex.getMessage() != null ? ex.getMessage() : "알 수 없는 오류가 발생했습니다."; ErrorResponse errorResponse = new ErrorResponse(errorMessage, ex.getErrorCode().getStatus().value()); @@ -42,4 +56,25 @@ protected ResponseEntity handleDataIntegrityViolationException(Da ErrorResponse response = new ErrorResponse(ResponseConstants.ErrorMessage.INVALID_INPUT, ResponseConstants.StatusCode.BAD_REQUEST); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + @ExceptionHandler(Exception.class) + protected ResponseEntity handleGeneralException(HttpServletRequest request, Exception ex) { + // 예외를 로그로 기록 + logService.recordErrorLog("처리되지 않은 예외 발생", ex); + + HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; + String errorMessage = "서버 내부 오류가 발생했습니다."; + + if (ex instanceof MissingServletRequestParameterException) { + status = HttpStatus.BAD_REQUEST; + errorMessage = "필수 파라미터가 누락되었습니다."; + } else if (ex instanceof MethodArgumentNotValidException) { + status = HttpStatus.BAD_REQUEST; + errorMessage = "요청 파라미터가 유효하지 않습니다."; + } + // 필요한 다른 예외 처리 추가 + + ErrorResponse response = new ErrorResponse(errorMessage, status.value()); + return new ResponseEntity<>(response, status); + } } + diff --git a/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java b/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java index 1cdcfd44..8e66810e 100644 --- a/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java +++ b/src/main/java/com/splanet/splanet/log/interceptor/ApiLoggingInterceptor.java @@ -36,6 +36,18 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons return true; } + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + Long userId = (Long) request.getSession().getAttribute("userId"); + String deviceId = (String) request.getSession().getAttribute("deviceId"); + String requestPath = request.getRequestURI(); + String headers = getLoggableHeadersAsString(request); + int statusCode = response.getStatus(); + + // 로그 기록 + logService.recordApiRequestLog(userId, deviceId, requestPath, headers, statusCode); + } + private String getLoggableHeadersAsString(HttpServletRequest request) { StringBuilder headers = new StringBuilder(); loggableHeaders.forEach(headerName -> { diff --git a/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java b/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java index 8b9aa67f..70988a0c 100644 --- a/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java +++ b/src/main/java/com/splanet/splanet/oauth/OAuth2AuthenticationSuccessHandler.java @@ -91,15 +91,13 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo .queryParam("refresh", refreshToken) .queryParam("deviceId", deviceId) .build().toUriString(); - + // 토큰 정보는 제외하고 리다이렉트 URL만 로그에 기록 + int statusCode = response.getStatus(); // 상태 코드 가져오기 + logService.recordApiRequestLog(user.getId(), deviceId, "Redirected to: " + redirectUrl, headers, statusCode); // 응답 커밋 상태 확인 후 리다이렉트 if (!response.isCommitted()) { try { response.sendRedirect(redirectUrlWithParams); - - // 토큰 정보는 제외하고 리다이렉트 URL만 로그에 기록 - int statusCode = response.getStatus(); // 상태 코드 가져오기 - logService.recordApiRequestLog(user.getId(), deviceId, "Redirected to: " + redirectUrl, headers, statusCode); } catch (IOException e) { logService.recordErrorLog("Failed to redirect after successful authentication", e); }