From ad64a64ad97b0e59ead1d739fd80756c62761c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=A7=84?= Date: Sat, 7 Sep 2024 11:22:11 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9A=94=EC=B2=AD=20URI=20=EB=A1=9C?= =?UTF-8?q?=EA=B9=85=20AOP=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ktbgratitudediary/config/AopConfig.java | 30 +++++++++++++++++++ .../filter/JwtAuthenticationFilter.java | 9 +----- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/main/java/ktb/hackathon/ktbgratitudediary/config/AopConfig.java diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/config/AopConfig.java b/src/main/java/ktb/hackathon/ktbgratitudediary/config/AopConfig.java new file mode 100644 index 0000000..330d4e9 --- /dev/null +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/config/AopConfig.java @@ -0,0 +1,30 @@ +package ktb.hackathon.ktbgratitudediary.config; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +@Slf4j +@Aspect +@Configuration +public class AopConfig { + + @Before("execution(* ktb.hackathon.ktbgratitudediary.controller.*(..))") + public void logBefore(JoinPoint joinPoint) { + // HTTP 요청 정보를 얻기 위해 RequestContextHolder 사용 + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (attributes != null) { + HttpServletRequest request = attributes.getRequest(); + + String requestURL = request.getRequestURI(); + String methodType = request.getMethod(); + + log.info("HTTP Request - {} / {}", methodType, requestURL); + } + } +} diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java b/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java index 62087c6..5c2cd8a 100644 --- a/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java @@ -26,16 +26,9 @@ public class JwtAuthenticationFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; - log.info("RequestURI: {}", request.getRequestURI()); String token = resolveToken(request); - if(token == null) { - log.error("AccessToken is Null"); - ((HttpServletResponse) servletResponse) - .sendError(HttpServletResponse.SC_UNAUTHORIZED, "AccessToken is Null"); - return; - } - if (jwtTokenProvider.validateToken(token)) { + if (token != null && jwtTokenProvider.validateToken(token)) { var authentication = jwtTokenProvider.getAuthentication(token); SecurityContextHolder.getContext().setAuthentication(authentication); }