From 8dc084da73748648ef36d18d6ecef18c782f26cc Mon Sep 17 00:00:00 2001 From: TaeHoon0 Date: Thu, 25 Jul 2024 17:26:35 +0900 Subject: [PATCH] =?UTF-8?q?[#139]=20refactor=20:=20=EC=95=A1=EC=84=B8?= =?UTF-8?q?=EC=8A=A4=20=ED=86=A0=ED=81=B0=20=EB=A7=8C=EB=A3=8C=EC=8B=9C=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/jwt/JwtAuthenticationFilter.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/example/CatchStudy/global/jwt/JwtAuthenticationFilter.java b/src/main/java/com/example/CatchStudy/global/jwt/JwtAuthenticationFilter.java index 3fcaa68..e4c25d8 100644 --- a/src/main/java/com/example/CatchStudy/global/jwt/JwtAuthenticationFilter.java +++ b/src/main/java/com/example/CatchStudy/global/jwt/JwtAuthenticationFilter.java @@ -52,15 +52,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse SecurityContextHolder.getContext().setAuthentication(authentication); // 검증 후 security context 인증 정보 저장 } catch (ExpiredJwtException e) { // 만료 에러 발생 // refreshToken 존재시 - if(jwtUtil.validateRefreshToken(accessToken)) { - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - response.setContentType("application/json"); - response.setCharacterEncoding("UTF-8"); - - Response errorResponse = Response.error("401", ErrorCode.EXPIRED_ACCESS_TOKEN.getMessage()); - ObjectMapper objectMapper = new ObjectMapper(); - String jsonResponse = objectMapper.writeValueAsString(errorResponse); - response.getWriter().write(jsonResponse); + if (jwtUtil.validateRefreshToken(accessToken)) { + sendErrorResponse(response, "401", ErrorCode.EXPIRED_ACCESS_TOKEN.getMessage()); + } else { + sendErrorResponse(response, "403", ErrorCode.EXPIRED_LOGIN_ERROR.getMessage()); } return; @@ -78,4 +73,16 @@ private String resolveToken(HttpServletRequest request) { return null; } + + private static void sendErrorResponse(HttpServletResponse response, String statusCode, String message) throws IOException { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + + Response errorResponse = Response.error(statusCode, message); + ObjectMapper objectMapper = new ObjectMapper(); + String jsonResponse = objectMapper.writeValueAsString(errorResponse); + response.getWriter().write(jsonResponse); + } + }