-
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.
refactor: JWT 에러 응답 리팩토링 및 인가 인증 예최 처리 코드 수정 (#103)
* refactor: 기타 코드 리팩토링( 피드백 반영 ) (#101) * chore: JWT 관련 의존성 변경 (#101) * refactor: JWT 및 인증 관련 로직 리팩토링 (#101) * test: 테스트 코드 및 설정 관련 변경(#101) * refactor: ObjectMapper Autowired 로 주입 (#101) * refactor: AuthService 반환 타입 Optional<User> -> User 변경 (#101)
- Loading branch information
1 parent
9e59ac8
commit 7470f70
Showing
11 changed files
with
114 additions
and
75 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
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
29 changes: 19 additions & 10 deletions
29
src/main/java/net/teumteum/core/security/filter/JwtAccessDeniedHandler.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 |
---|---|---|
@@ -1,31 +1,40 @@ | ||
package net.teumteum.core.security.filter; | ||
|
||
import jakarta.servlet.ServletException; | ||
import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.teumteum.core.error.ErrorResponse; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
import static jakarta.servlet.http.HttpServletResponse.SC_FORBIDDEN; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, | ||
HttpServletResponse response, | ||
AccessDeniedException accessDeniedException | ||
) throws IOException, ServletException { | ||
HttpServletResponse response, | ||
AccessDeniedException accessDeniedException | ||
) throws IOException { | ||
this.sendUnAuthorizedError(response, accessDeniedException); | ||
} | ||
|
||
private void sendUnAuthorizedError(HttpServletResponse response, | ||
Exception exception) throws IOException { | ||
Exception exception) throws IOException { | ||
response.setStatus(SC_FORBIDDEN); | ||
OutputStream os = response.getOutputStream(); | ||
log.error("Responding with unauthorized error. Message - {}", exception.getMessage()); | ||
response.sendError(SC_FORBIDDEN, exception.getMessage()); | ||
objectMapper.writeValue(os, ErrorResponse.of("인가 과정에서 오류가 발생했습니다.")); | ||
os.flush(); | ||
} | ||
} |
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
Oops, something went wrong.