-
Notifications
You must be signed in to change notification settings - Fork 1
[배포] 토큰 만료 처리 #346
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
[배포] 토큰 만료 처리 #346
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()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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 void sendErrorResponse(HttpServletResponse response, ErrorResponse errorResponse) throws IOException {
response.setStatus(errorResponse.getStatus());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
objectMapper.writeValue(response.getWriter(), errorResponse);
}이렇게 하면 각 |
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
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.
CustomRuntimeException을 처리할 때 응답 본문의status필드에는ex.getStatus().value()를 사용하면서, 실제 HTTP 응답 상태 코드는HttpStatus.UNAUTHORIZED.value()로 고정하고 있습니다. 이 두 값은 일치해야 합니다.response.setStatus()호출 시에도ex.getStatus().value()를 사용하여 예외에 정의된 정확한 상태 코드가 반환되도록 수정해야 합니다.