From b16008c0d1da1c497d42a8ac488768438e2ffef1 Mon Sep 17 00:00:00 2001 From: ckx6954 Date: Tue, 18 Mar 2025 20:10:45 +0900 Subject: [PATCH] =?UTF-8?q?handler=20Error=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/code/UserErrorCode.java | 8 ++--- .../handler/MemberAuthFailureHandler.java | 30 ++++++------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java b/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java index c7a4be5..42cd8c8 100644 --- a/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java +++ b/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java @@ -26,10 +26,10 @@ public enum UserErrorCode implements ErrorCode { NOT_AUTH_USER(HttpStatus.UNAUTHORIZED ,"로그인이 필요합니다."), INACTIVE_USER(HttpStatus.FORBIDDEN,"권한이 필요합니다."), - USER_NOT_FOUND(HttpStatus.UNAUTHORIZED,"아이디 혹은 비밀번호가 틀렸습니다."), - NOT_AUTHORIZED(HttpStatus.UNAUTHORIZED,"비인가 계정입니다."), - EXIT_USER(HttpStatus.UNAUTHORIZED,"탈퇴한 계정입니다."), - BAD_CREDENTIAL(HttpStatus.UNAUTHORIZED, "아이디 혹은 비밀번호 문제") + USER_NOT_FOUND(10001,HttpStatus.UNAUTHORIZED,"아이디 혹은 비밀번호가 틀렸습니다."), + NOT_AUTHORIZED(10002,HttpStatus.UNAUTHORIZED,"비인가 계정입니다."), + EXIT_USER(10003,HttpStatus.UNAUTHORIZED,"탈퇴한 계정입니다."), + BAD_CREDENTIAL(10004,HttpStatus.UNAUTHORIZED, "아이디 혹은 비밀번호 문제") ; private final long code; diff --git a/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java b/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java index 5bf065c..134b19c 100644 --- a/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java +++ b/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java @@ -15,6 +15,8 @@ package io.company.localhost.common.security.handler; import com.fasterxml.jackson.databind.ObjectMapper; +import io.company.localhost.common.dto.ApiResponse; +import io.company.localhost.common.exception.code.UserErrorCode; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.http.MediaType; @@ -24,8 +26,6 @@ import org.springframework.security.web.authentication.AuthenticationFailureHand import org.springframework.stereotype.Component; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; @Component("failHandler") public class MemberAuthFailureHandler implements AuthenticationFailureHandler { @@ -35,31 +35,19 @@ public class MemberAuthFailureHandler implements AuthenticationFailureHandler { ObjectMapper mapper = new ObjectMapper(); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(MediaType.APPLICATION_JSON_VALUE); - - Map responseMap = new HashMap<>(); - responseMap.put("success", false); // 로그인 실패 표시 - + String message = exception.getMessage(); - String errorCode; - String errorMessage; + + ApiResponse res = UserErrorCode.BAD_CREDENTIAL.getApiResponse(); if (exception instanceof BadCredentialsException || message.startsWith("NOT_FOUND")) { - errorCode = "USER_NOT_FOUND"; - errorMessage = "아이디 또는 비밀번호가 일치하지 않습니다."; + res = UserErrorCode.USER_NOT_FOUND.getApiResponse(); } else if (message.startsWith("NOT_AUTHORIZED")) { - errorCode = "NOT_AUTHORIZED"; - errorMessage = "접근 권한이 없습니다."; + res = UserErrorCode.NOT_AUTH_USER.getApiResponse(); } else if (message.startsWith("EXIT")) { - errorCode = "EXIT_USER"; - errorMessage = "탈퇴한 사용자입니다."; - } else { - errorCode = "BAD_CREDENTIAL"; - errorMessage = "인증에 실패했습니다."; + res = UserErrorCode.EXIT_USER.getApiResponse(); } - - responseMap.put("code", errorCode); - responseMap.put("message", errorMessage); - response.getWriter().write(mapper.writeValueAsString(responseMap)); + response.getWriter().write(mapper.writeValueAsString(res)); } }