parent
17bb8868b6
commit
ec67f89eb4
@ -0,0 +1,48 @@
|
|||||||
|
package kr.re.etri.autoflow.common;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class LoggingInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
|
||||||
|
@NonNull Object handler) {
|
||||||
|
String userId = request.getHeader("userId");
|
||||||
|
String path = request.getRequestURI();
|
||||||
|
String method = request.getMethod();
|
||||||
|
|
||||||
|
//정적인 리소스 로깅 안하게 처리
|
||||||
|
if (!(handler instanceof HandlerMethod)) {
|
||||||
|
log.debug(">>> Static resource or non-handler request. API: {}, method: {}, userId: {}", path, method, userId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
String handlerName = ((HandlerMethod) handler).getBeanType().getSimpleName();
|
||||||
|
log.info(">>> {} invoked. API: {}, method: {}, userId: {}", handlerName, path, method, userId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
|
||||||
|
@NonNull Object handler, @Nullable ModelAndView modelAndView) {
|
||||||
|
log.info("API Response: {}, userId: {}, status: {}", request.getRequestURI(),
|
||||||
|
request.getHeader("userId"), response.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
|
||||||
|
@NonNull Object handler, @Nullable Exception ex) {
|
||||||
|
if (ex != null) {
|
||||||
|
log.error("Error during request: {}", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
package kr.re.etri.autoflow.common;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfiguration implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
registry.addMapping("/**")
|
||||||
|
.allowedOrigins("*")
|
||||||
|
.allowedMethods(HttpMethod.GET.name(),
|
||||||
|
HttpMethod.HEAD.name(),
|
||||||
|
HttpMethod.POST.name(),
|
||||||
|
HttpMethod.PUT.name(),
|
||||||
|
HttpMethod.DELETE.name())
|
||||||
|
.maxAge(3600);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(new LoggingInterceptor())
|
||||||
|
.addPathPatterns("/**"); // Intercepts all requests
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue