-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
106 additions
and
9 deletions.
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/sch/chekirout/auth/exception/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,40 @@ | ||
package com.sch.chekirout.auth.exception; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.sch.chekirout.common.exception.CustomAuthenticationException; | ||
import com.sch.chekirout.global.presentation.ErrorResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { | ||
Throwable cause = authException.getCause(); | ||
|
||
if (cause instanceof CustomAuthenticationException) { | ||
CustomAuthenticationException customException = (CustomAuthenticationException) cause; | ||
|
||
response.setStatus(HttpStatus.BAD_REQUEST.value()); | ||
response.setContentType("application/json;charset=UTF-8"); | ||
|
||
ErrorResponse errorResponse = new ErrorResponse( | ||
customException.getMessage(), | ||
customException.getErrorCode(), | ||
HttpStatus.BAD_REQUEST.value() | ||
); | ||
|
||
response.getWriter().write(new ObjectMapper().writeValueAsString(errorResponse)); | ||
} else { | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); | ||
} | ||
} | ||
} |
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
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/sch/chekirout/common/exception/CustomAuthenticationException.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,16 @@ | ||
package com.sch.chekirout.common.exception; | ||
|
||
import org.springframework.security.core.AuthenticationException; | ||
|
||
public class CustomAuthenticationException extends AuthenticationException { | ||
private final ErrorCode errorCode; | ||
|
||
public CustomAuthenticationException(String message, ErrorCode errorCode) { | ||
super(message); | ||
this.errorCode = errorCode; | ||
} | ||
|
||
public ErrorCode getErrorCode() { | ||
return errorCode; | ||
} | ||
} |
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
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
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
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