Skip to content

Commit

Permalink
feat: 로깅 AOP 등록
Browse files Browse the repository at this point in the history
  • Loading branch information
maiload committed Sep 7, 2024
1 parent b621c30 commit 1b93564
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public CorsConfigurationSource corsConfigurationSource() {
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); // 허용할 메서드 설정
configuration.setAllowedHeaders(List.of("Authorization", "Content-Type")); // 허용할 헤더 설정
configuration.setAllowCredentials(true); // 쿠키 등 자격 증명 허용
// configuration.setAllowCredentials(false);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration); // 모든 경로에 대해 CORS 설정 적용
Expand Down

0 comments on commit 1b93564

Please sign in to comment.