handler Error 수정

This commit is contained in:
ckx6954 2025-03-18 20:10:45 +09:00
parent e411a0a567
commit b16008c0d1
2 changed files with 13 additions and 25 deletions

View File

@ -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;

View File

@ -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 {
@ -36,30 +36,18 @@ public class MemberAuthFailureHandler implements AuthenticationFailureHandler {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
Map<String, Object> 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));
}
}