-
Notifications
You must be signed in to change notification settings - Fork 2
MOSU-344 refactor: 토큰 만료 핸들링 #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,19 +1,26 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| package life.mosu.mosuserver.global.filter; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import io.jsonwebtoken.ExpiredJwtException; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.servlet.FilterChain; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.servlet.ServletException; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.servlet.http.HttpServletRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.servlet.http.HttpServletResponse; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import life.mosu.mosuserver.global.exception.CustomRuntimeException; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import life.mosu.mosuserver.global.exception.ErrorResponse; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.RequiredArgsConstructor; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.http.HttpStatus; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.http.MediaType; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Component; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.web.filter.OncePerRequestFilter; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @Component | ||||||||||||||||||||||||||||||||||||||||||||||||
| @RequiredArgsConstructor | ||||||||||||||||||||||||||||||||||||||||||||||||
| public class TokenExceptionFilter extends OncePerRequestFilter { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private final ObjectMapper objectMapper; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||
| protected void doFilterInternal( | ||||||||||||||||||||||||||||||||||||||||||||||||
| final HttpServletRequest request, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -22,11 +29,43 @@ protected void doFilterInternal( | |||||||||||||||||||||||||||||||||||||||||||||||
| ) throws ServletException, IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||
| filterChain.doFilter(request, response); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (CustomRuntimeException exception) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.setContentType("application/json"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (CustomRuntimeException ex) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| ErrorResponse errorResponse = ErrorResponse.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .status(ex.getStatus().value()) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .message(ex.getMessage()) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .code(ex.getCode()) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .build(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| response.setStatus(HttpStatus.UNAUTHORIZED.value()); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.setCharacterEncoding("UTF-8"); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| objectMapper.writeValue(response.getWriter(), errorResponse); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (ExpiredJwtException ex) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ErrorResponse errorResponse = ErrorResponse.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .status(HttpStatus.NOT_ACCEPTABLE.value()) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .message("토큰이 만료되었습니다.") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .code("TOKEN_EXPIRED") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .build(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| response.setStatus(HttpStatus.NOT_ACCEPTABLE.value()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.setCharacterEncoding("UTF-8"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.sendError(exception.getStatus().value(), exception.getMessage()); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| objectMapper.writeValue(response.getWriter(), errorResponse); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 만료된 토큰에 대해
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (Exception ex) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ErrorResponse errorResponse = ErrorResponse.builder() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .status(HttpStatus.INTERNAL_SERVER_ERROR.value()) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .message("서버 오류가 발생했습니다.") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .code("INTERNAL_SERVER_ERROR") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .build(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||||||||||||||||||||||||||||||||||||||||||||||||
| response.setCharacterEncoding("UTF-8"); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| objectMapper.writeValue(response.getWriter(), errorResponse); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재
아래는 리팩토링 예시입니다. // private helper method
private void writeErrorResponse(HttpServletResponse res, HttpStatus status, String msg, String code) throws IOException {
res.setStatus(status.value());
res.setContentType(MediaType.APPLICATION_JSON_VALUE);
res.setCharacterEncoding("UTF-8");
ErrorResponse errRes = ErrorResponse.builder()
.status(status.value()).message(msg).code(code).build();
objectMapper.writeValue(res.getWriter(), errRes);
}
// catch (Exception ex) block
// log.error("Unhandled exception in filter chain", ex);
// writeErrorResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류가 발생했습니다.", "INTERNAL_SERVER_ERROR"); |
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이
catch (ExpiredJwtException exception)블록은 예외를 잡아서 아무런 추가 작업 없이 다시 던지고 있습니다. 이는 불필요하며 코드를 복잡하게 만듭니다. 이catch블록을 제거해도 동작은 동일하게 유지되면서 코드가 더 간결해집니다.