-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Team-A-Mango/42-fix-error-bug
403으로 반환되던 처리되지 않은 일반 예외를 커스텀 인증 예외 처리로 수정
- Loading branch information
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/mango/amango/global/security/CustomAuthenticationEntryPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.mango.amango.global.security; | ||
|
||
import com.mango.amango.global.exception.CustomErrorCode; | ||
import com.mango.amango.global.exception.CustomException; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.security.authentication.InsufficientAuthenticationException; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerExceptionResolver; | ||
|
||
import java.io.IOException; | ||
|
||
@Primary | ||
@Component | ||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private final HandlerExceptionResolver resolver; | ||
|
||
public CustomAuthenticationEntryPoint(@Qualifier("handlerExceptionResolver") HandlerExceptionResolver resolver) { | ||
this.resolver = resolver; | ||
} | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
resolver.resolveException(request, response, null, getCustomException(response, authException)); | ||
} | ||
|
||
private CustomException getCustomException(HttpServletResponse response, AuthenticationException authException) { | ||
if (isSuccessResponse(response) && isAuthError(authException)) { | ||
return new CustomException(CustomErrorCode.UNAUTHORIZED); | ||
|
||
} else { | ||
return switch (response.getStatus()) { | ||
case 401 -> new CustomException(CustomErrorCode.UNAUTHORIZED); | ||
case 403 -> new CustomException(CustomErrorCode.FORBIDDEN); | ||
case 404 -> new CustomException(CustomErrorCode.NOT_FOUND); | ||
case 500 -> new CustomException(CustomErrorCode.INTERNAL_SERVER_ERROR); | ||
default -> throw authException; | ||
|
||
}; | ||
} | ||
} | ||
|
||
private boolean isSuccessResponse(HttpServletResponse response) { | ||
return response.getStatus() == 200 || response.getStatus() == 201; | ||
} | ||
|
||
private boolean isAuthError(AuthenticationException authException) { | ||
return authException instanceof InsufficientAuthenticationException; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters