Skip to content

Commit

Permalink
feat: 요청 URI 로깅 AOP 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
maiload committed Sep 7, 2024
1 parent b621c30 commit ad64a64
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit ad64a64

Please sign in to comment.