-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 인증되지 않은 사용자 요청에 대한 예외를 처리하는 AuthenticationEntryPoint 구현체 구현
- Loading branch information
Showing
3 changed files
with
43 additions
and
2 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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/umc/naoman/global/security/handler/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,37 @@ | ||
package com.umc.naoman.global.security.handler; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.umc.naoman.global.error.ErrorResponse; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
|
||
import static com.umc.naoman.global.error.code.GlobalErrorCode.UNAUTHORIZED; | ||
|
||
@Component | ||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) throws IOException, ServletException { | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setStatus(UNAUTHORIZED.getStatus()); | ||
response.setCharacterEncoding(Charset.defaultCharset().name()); | ||
|
||
ErrorResponse errorResponse = ErrorResponse.builder() | ||
.status(response.getStatus()) | ||
.code(UNAUTHORIZED.getMessage()) | ||
.message(authException.getMessage()) | ||
.data(null) | ||
.build(); | ||
|
||
response.getWriter().write(objectMapper.writeValueAsString(errorResponse)); | ||
} | ||
} |