로그인 실패 exception 처리, saveErrorLogData 오타 수정

This commit is contained in:
2025-12-08 11:34:55 +09:00
parent 55edeaf300
commit 603f510973
3 changed files with 214 additions and 179 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -48,7 +49,7 @@ public class GlobalExceptionHandler {
log.warn("[EntityNotFoundException] resource :{} ", e.getMessage());
String codeName = "NOT_FOUND_DATA";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf("UNPROCESSABLE_ENTITY"),
@@ -69,7 +70,7 @@ public class GlobalExceptionHandler {
log.warn("[HttpMessageNotReadableException] resource :{} ", e.getMessage());
String codeName = "BAD_REQUEST";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -90,7 +91,7 @@ public class GlobalExceptionHandler {
log.warn("[NoSuchElementException] resource :{} ", e.getMessage());
String codeName = "NOT_FOUND_DATA";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -111,7 +112,7 @@ public class GlobalExceptionHandler {
log.warn("[handlerIllegalArgumentException] resource :{} ", e.getMessage());
String codeName = "BAD_REQUEST";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -132,7 +133,7 @@ public class GlobalExceptionHandler {
log.warn("[DataIntegrityViolationException] resource :{} ", e.getMessage());
String codeName = "DATA_INTEGRITY_ERROR";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf("UNPROCESSABLE_ENTITY"),
@@ -153,7 +154,7 @@ public class GlobalExceptionHandler {
log.warn("[MethodArgumentNotValidException] resource :{} ", e.getMessage());
String codeName = "BAD_REQUEST";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -174,7 +175,7 @@ public class GlobalExceptionHandler {
log.warn("[AccessDeniedException] resource :{} ", e.getMessage());
String codeName = "UNAUTHORIZED";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -188,6 +189,7 @@ public class GlobalExceptionHandler {
errorLog.getId());
}
@ResponseStatus(HttpStatus.BAD_GATEWAY)
@ExceptionHandler(HttpServerErrorException.BadGateway.class)
public ApiResponseDto<String> handlerHttpServerErrorException(
@@ -195,7 +197,7 @@ public class GlobalExceptionHandler {
log.warn("[HttpServerErrorException] resource :{} ", e.getMessage());
String codeName = "BAD_GATEWAY";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -217,7 +219,7 @@ public class GlobalExceptionHandler {
String codeName = "UNPROCESSABLE_ENTITY";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -252,7 +254,7 @@ public class GlobalExceptionHandler {
}
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf("BAD_REQUEST"),
@@ -275,7 +277,7 @@ public class GlobalExceptionHandler {
String codeName = "NOT_FOUND_USER";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf("BAD_REQUEST"),
@@ -298,7 +300,7 @@ public class GlobalExceptionHandler {
String codeName = "DUPLICATE_DATA";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf("CONFLICT"),
@@ -312,6 +314,35 @@ public class GlobalExceptionHandler {
errorLog.getId());
}
@ExceptionHandler(BadCredentialsException.class)
public ResponseEntity<ApiResponseDto<String>> handleBadCredentials(
BadCredentialsException e, HttpServletRequest request
) {
log.warn("[BadCredentialsException] resource : {} ", e.getMessage());
String codeName = "UNAUTHORIZED";
ErrorLogEntity errorLog =
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
ErrorLogDto.LogErrorLevel.WARNING,
e.getStackTrace());
ApiResponseDto<String> body =
ApiResponseDto.createException(
ApiResponseCode.getCode(codeName),
ApiResponseCode.getMessage(codeName),
HttpStatus.valueOf(codeName),
errorLog.getId());
return ResponseEntity
.status(HttpStatus.UNAUTHORIZED) // 🔥 여기서 401 지정
.body(body);
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public ApiResponseDto<String> handlerRuntimeException(
@@ -320,7 +351,7 @@ public class GlobalExceptionHandler {
String codeName = "INTERNAL_SERVER_ERROR";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -341,7 +372,7 @@ public class GlobalExceptionHandler {
String codeName = "INTERNAL_SERVER_ERROR";
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf(codeName),
@@ -365,7 +396,7 @@ public class GlobalExceptionHandler {
* @param stackTrace : 에러 내용
* @return : insert하고 결과로 받은 Entity
*/
private ErrorLogEntity saveErrerLogData(
private ErrorLogEntity saveErrorLogData(
HttpServletRequest request,
ApiResponseCode errorCode,
HttpStatus httpStatus,
@@ -422,7 +453,7 @@ public class GlobalExceptionHandler {
ApiResponseCode apiCode = ApiResponseCode.getCode(codeName);
ErrorLogEntity errorLog =
saveErrerLogData(
saveErrorLogData(
request, apiCode, status, ErrorLogDto.LogErrorLevel.WARNING, e.getStackTrace());
ApiResponseDto<String> body =

View File

@@ -86,7 +86,7 @@ public class AuthController {
.build();
response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString());
return ApiResponseDto.createOK(new TokenResponse(accessToken));
return ApiResponseDto.ok(new TokenResponse(accessToken));
}
@PostMapping("/refresh")

View File

@@ -1,6 +1,7 @@
package com.kamco.cd.kamcoback.members.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@@ -10,7 +11,10 @@ import lombok.ToString;
@ToString(exclude = "password")
public class SignInRequest {
@Schema(description = "사번", example = "11111")
private String username;
@Schema(description = "비밀번호", example = "kamco1234!")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
}