From 886fff0392bb9b7dd96092622b26dc79f0a787ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lkd9125=28=EC=9D=B4=EA=B2=BD=EB=8F=84=29?= Date: Fri, 2 Feb 2024 14:43:19 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20401=20=EC=9D=B8=EC=A6=9D=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EB=B0=9C=EC=83=9D=EC=8B=9C=20Body=EC=97=90=20statu?= =?UTF-8?q?s=EA=B0=92=20=EC=B6=94=EA=B0=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/JwtAuthenticationEntryPoint.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/filter/JwtAuthenticationEntryPoint.java b/pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/filter/JwtAuthenticationEntryPoint.java index 815b81bd..bf3a0d60 100644 --- a/pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/filter/JwtAuthenticationEntryPoint.java +++ b/pav-server/src/main/java/com/palnet/biz/api/acnt/jwt/filter/JwtAuthenticationEntryPoint.java @@ -2,24 +2,42 @@ package com.palnet.biz.api.acnt.jwt.filter; import java.io.IOException; import java.io.Serializable; +import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.palnet.comn.utils.JsonUtils; +import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.stereotype.Component; -//허가되지 않은 사용자라면, 접근 불가 메세지를 띄워 리소스 정보획득을 못하게 막아줍니다. @Component +@Slf4j public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable { private static final long serialVersionUID = -7858869558953243875L; + /** + * 인증되지 않은 사용자의 Exception을 Handling하는 메소드입니다. + * @param request that resulted in an AuthenticationException + * @param response so that the user agent can begin authentication + * @param authException that caused the invocation + * @throws IOException + */ @Override - public void commence(HttpServletRequest request, HttpServletResponse response, - AuthenticationException authException) throws IOException { - - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); + public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { + + HashMap jsonResponseBody = new HashMap<>(); + jsonResponseBody.put("status", HttpServletResponse.SC_UNAUTHORIZED); + + String jsonBody = JsonUtils.toJson(jsonResponseBody); + + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + response.getWriter().write(jsonBody); } } \ No newline at end of file