handler Error 수정
This commit is contained in:
parent
e411a0a567
commit
b16008c0d1
@ -26,10 +26,10 @@ public enum UserErrorCode implements ErrorCode {
|
|||||||
|
|
||||||
NOT_AUTH_USER(HttpStatus.UNAUTHORIZED ,"로그인이 필요합니다."),
|
NOT_AUTH_USER(HttpStatus.UNAUTHORIZED ,"로그인이 필요합니다."),
|
||||||
INACTIVE_USER(HttpStatus.FORBIDDEN,"권한이 필요합니다."),
|
INACTIVE_USER(HttpStatus.FORBIDDEN,"권한이 필요합니다."),
|
||||||
USER_NOT_FOUND(HttpStatus.UNAUTHORIZED,"아이디 혹은 비밀번호가 틀렸습니다."),
|
USER_NOT_FOUND(10001,HttpStatus.UNAUTHORIZED,"아이디 혹은 비밀번호가 틀렸습니다."),
|
||||||
NOT_AUTHORIZED(HttpStatus.UNAUTHORIZED,"비인가 계정입니다."),
|
NOT_AUTHORIZED(10002,HttpStatus.UNAUTHORIZED,"비인가 계정입니다."),
|
||||||
EXIT_USER(HttpStatus.UNAUTHORIZED,"탈퇴한 계정입니다."),
|
EXIT_USER(10003,HttpStatus.UNAUTHORIZED,"탈퇴한 계정입니다."),
|
||||||
BAD_CREDENTIAL(HttpStatus.UNAUTHORIZED, "아이디 혹은 비밀번호 문제")
|
BAD_CREDENTIAL(10004,HttpStatus.UNAUTHORIZED, "아이디 혹은 비밀번호 문제")
|
||||||
;
|
;
|
||||||
|
|
||||||
private final long code;
|
private final long code;
|
||||||
|
|||||||
@ -15,6 +15,8 @@
|
|||||||
package io.company.localhost.common.security.handler;
|
package io.company.localhost.common.security.handler;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@ -24,8 +26,6 @@ import org.springframework.security.web.authentication.AuthenticationFailureHand
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Component("failHandler")
|
@Component("failHandler")
|
||||||
public class MemberAuthFailureHandler implements AuthenticationFailureHandler {
|
public class MemberAuthFailureHandler implements AuthenticationFailureHandler {
|
||||||
@ -36,30 +36,18 @@ public class MemberAuthFailureHandler implements AuthenticationFailureHandler {
|
|||||||
response.setStatus(HttpServletResponse.SC_OK);
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
|
||||||
Map<String, Object> responseMap = new HashMap<>();
|
|
||||||
responseMap.put("success", false); // 로그인 실패 표시
|
|
||||||
|
|
||||||
String message = exception.getMessage();
|
String message = exception.getMessage();
|
||||||
String errorCode;
|
|
||||||
String errorMessage;
|
ApiResponse<?> res = UserErrorCode.BAD_CREDENTIAL.getApiResponse();
|
||||||
|
|
||||||
if (exception instanceof BadCredentialsException || message.startsWith("NOT_FOUND")) {
|
if (exception instanceof BadCredentialsException || message.startsWith("NOT_FOUND")) {
|
||||||
errorCode = "USER_NOT_FOUND";
|
res = UserErrorCode.USER_NOT_FOUND.getApiResponse();
|
||||||
errorMessage = "아이디 또는 비밀번호가 일치하지 않습니다.";
|
|
||||||
} else if (message.startsWith("NOT_AUTHORIZED")) {
|
} else if (message.startsWith("NOT_AUTHORIZED")) {
|
||||||
errorCode = "NOT_AUTHORIZED";
|
res = UserErrorCode.NOT_AUTH_USER.getApiResponse();
|
||||||
errorMessage = "접근 권한이 없습니다.";
|
|
||||||
} else if (message.startsWith("EXIT")) {
|
} else if (message.startsWith("EXIT")) {
|
||||||
errorCode = "EXIT_USER";
|
res = UserErrorCode.EXIT_USER.getApiResponse();
|
||||||
errorMessage = "탈퇴한 사용자입니다.";
|
|
||||||
} else {
|
|
||||||
errorCode = "BAD_CREDENTIAL";
|
|
||||||
errorMessage = "인증에 실패했습니다.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
responseMap.put("code", errorCode);
|
response.getWriter().write(mapper.writeValueAsString(res));
|
||||||
responseMap.put("message", errorMessage);
|
|
||||||
|
|
||||||
response.getWriter().write(mapper.writeValueAsString(responseMap));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user