Skip to content

Commit feb828f

Browse files
committed
refactor: 예외 응답 함수 추출
1 parent e8632fc commit feb828f

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

src/main/java/com/example/solidconnection/config/security/JwtAuthenticationEntryPoint.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import java.io.IOException;
1414

15-
import static com.example.solidconnection.custom.exception.ErrorCode.ACCESS_TOKEN_EXPIRED;
1615
import static com.example.solidconnection.custom.exception.ErrorCode.AUTHENTICATION_FAILED;
1716

1817
@Component
@@ -25,24 +24,15 @@ public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
2524
public void commence(HttpServletRequest request, HttpServletResponse response,
2625
AuthenticationException authException) throws IOException {
2726
ErrorResponse errorResponse = new ErrorResponse(AUTHENTICATION_FAILED, authException.getMessage());
28-
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
29-
response.setContentType("application/json");
30-
response.setCharacterEncoding("UTF-8");
31-
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
27+
writeResponse(response, errorResponse);
3228
}
3329

34-
public void expiredCommence(HttpServletRequest request, HttpServletResponse response,
35-
AuthenticationException authException) throws IOException {
36-
ErrorResponse errorResponse = new ErrorResponse(new CustomException(ACCESS_TOKEN_EXPIRED));
37-
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
38-
response.setContentType("application/json");
39-
response.setCharacterEncoding("UTF-8");
40-
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
30+
public void customCommence(HttpServletResponse response, CustomException customException) throws IOException {
31+
ErrorResponse errorResponse = new ErrorResponse(customException);
32+
writeResponse(response, errorResponse);
4133
}
4234

43-
public void customCommence(HttpServletRequest request, HttpServletResponse response,
44-
CustomException customException) throws IOException {
45-
ErrorResponse errorResponse = new ErrorResponse(customException);
35+
private void writeResponse(HttpServletResponse response, ErrorResponse errorResponse) throws IOException {
4636
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
4737
response.setContentType("application/json");
4838
response.setCharacterEncoding("UTF-8");

src/main/java/com/example/solidconnection/config/security/JwtAuthenticationFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
6868
}
6969
} catch (JwtExpiredTokenException e) {
7070
SecurityContextHolder.clearContext();
71-
jwtAuthenticationEntryPoint.expiredCommence(request, response, e);
71+
jwtAuthenticationEntryPoint.commence(request, response, e);
7272
return;
7373
} catch (AuthenticationException e) {
7474
SecurityContextHolder.clearContext();
7575
jwtAuthenticationEntryPoint.commence(request, response, e);
7676
return;
7777
} catch (CustomException e) {
78-
jwtAuthenticationEntryPoint.customCommence(request, response, e);
78+
jwtAuthenticationEntryPoint.customCommence(response, e);
7979
return;
8080
}
8181
filterChain.doFilter(request, response); // 다음 필터로 요청과 응답 전달

0 commit comments

Comments
 (0)