fix: log trace 기능 업데이트
parent
f942f124ff
commit
3b2458d6cd
@ -1,187 +0,0 @@
|
||||
package kr.xit.core.spring.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.web.filter.CommonsRequestLoggingFilter;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import kr.xit.core.consts.Constants;
|
||||
import kr.xit.core.spring.config.auth.AuthentificationInterceptor;
|
||||
import kr.xit.core.spring.config.properties.CorsProperties;
|
||||
import kr.xit.core.spring.filter.LoggingFilter;
|
||||
import kr.xit.core.spring.filter.ReadableRequestWrapperFilter;
|
||||
import kr.xit.core.spring.resolver.CustomArgumentResolver;
|
||||
import kr.xit.core.spring.resolver.PageableArgumentResolver;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* description : Spring MVC 설정
|
||||
* - filter, interceptor
|
||||
* - AuthentificationInterceptor : 인증처리
|
||||
* - CommonsRequestLoggingFilter : request logging
|
||||
* - ReadableRequestWrapperFilter : post logging 처리를 위한 필터
|
||||
* - CORS
|
||||
* packageName : kr.xit.core.spring.config
|
||||
* fileName : WebMvcConfig
|
||||
* author : julim
|
||||
* date : 2023-04-28
|
||||
* ======================================================================
|
||||
* 변경일 변경자 변경 내용
|
||||
* ----------------------------------------------------------------------
|
||||
* 2023-04-28 julim 최초 생성
|
||||
*
|
||||
* </pre>
|
||||
* @see AuthentificationInterceptor
|
||||
* @see CommonsRequestLoggingFilter
|
||||
* @see ReadableRequestWrapperFilter
|
||||
* @see LoggingFilter
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
/**
|
||||
* logging exclude path
|
||||
*/
|
||||
@Value("${app.param.log.exclude-patterns}")
|
||||
private List<String> EXCLUDE_URL_REGEXS;
|
||||
|
||||
private final CorsProperties corsProperties;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new AuthentificationInterceptor())
|
||||
.addPathPatterns("/**/*")
|
||||
.excludePathPatterns(
|
||||
"/api/core/*"
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// RequestMappingHandlerMapping 설정 View Controller 추가
|
||||
// -------------------------------------------------------------
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/cmmn/validator.do")
|
||||
.setViewName("cmmn/validator");
|
||||
registry.addViewController("/").setViewName("forward:/index.html");
|
||||
}
|
||||
|
||||
//TODO :: ArgumentResolver add
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
resolvers.add(new CustomArgumentResolver());
|
||||
resolvers.add(new PageableArgumentResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* CommonsRequestLoggingFiler 등록
|
||||
* app.param.log.enabled: true시 로그 출력
|
||||
* @return
|
||||
*/
|
||||
@ConditionalOnProperty(value = "app.param.log.enabled", havingValue = "true", matchIfMissing = false)
|
||||
@Bean
|
||||
public FilterRegistrationBean requestLoggingFilter() {
|
||||
CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter(){
|
||||
@Override
|
||||
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
|
||||
String path = request.getServletPath();
|
||||
return EXCLUDE_URL_REGEXS.stream().anyMatch(regex -> path.matches(regex));
|
||||
}
|
||||
};
|
||||
loggingFilter.setIncludeClientInfo(true);
|
||||
loggingFilter.setIncludeHeaders(false);
|
||||
loggingFilter.setBeforeMessagePrefix("\n//========================== Request(Before) ================================\n");
|
||||
loggingFilter.setBeforeMessageSuffix("\n//===========================================================================");
|
||||
|
||||
loggingFilter.setIncludeQueryString(true);
|
||||
loggingFilter.setIncludePayload(true);
|
||||
loggingFilter.setMaxPayloadLength(1024* 1024);
|
||||
loggingFilter.setAfterMessagePrefix("\n//=========================== Request(After) ================================\n");
|
||||
loggingFilter.setAfterMessageSuffix("\n//===========================================================================");
|
||||
|
||||
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(loggingFilter);
|
||||
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||
bean.addUrlPatterns(Constants.API_URL_PATTERNS);
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* exclude pattern 지정
|
||||
* @return FilterRegistrationBean
|
||||
*/
|
||||
@ConditionalOnProperty(value = "app.param.log.custom.enabled", havingValue = "true", matchIfMissing = false)
|
||||
@Bean
|
||||
public FilterRegistrationBean loggingFilter() {
|
||||
Map<String, String> initMap = new HashMap<>();
|
||||
initMap.put("excludedUrls", StringUtils.join(EXCLUDE_URL_REGEXS,","));
|
||||
|
||||
FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>(new LoggingFilter());
|
||||
frb.setOrder(1);
|
||||
frb.addUrlPatterns(Constants.API_URL_PATTERNS);
|
||||
frb.setInitParameters(initMap);
|
||||
frb.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC);
|
||||
|
||||
return frb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post 요청시 request(stream) logging 처리를 위한 필터
|
||||
* @return
|
||||
*/
|
||||
@ConditionalOnProperty(value = "app.param.log.custom.enabled", havingValue = "true", matchIfMissing = false)
|
||||
@Bean
|
||||
public FilterRegistrationBean readableRequestWrapperFilter() {
|
||||
ReadableRequestWrapperFilter readableRequestWrapperFilter = new ReadableRequestWrapperFilter();
|
||||
|
||||
FilterRegistrationBean bean = new FilterRegistrationBean(readableRequestWrapperFilter);
|
||||
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||
bean.addUrlPatterns(Constants.API_URL_PATTERNS);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/api/**")
|
||||
.allowedOrigins(corsProperties.getAllowedOrigins().split(","))
|
||||
.allowedMethods(corsProperties.getAllowedMethods().split(","))
|
||||
.allowedHeaders(corsProperties.getAllowedHeaders().split(","))
|
||||
.allowCredentials(corsProperties.getAllowCredentials())
|
||||
.maxAge(corsProperties.getMaxAge())
|
||||
.exposedHeaders(corsProperties.getExposeHeader());
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
// * HandlerExceptionResolver 를 상속받은 resolver 등록
|
||||
// * @param resolvers the list of configured resolvers to extend
|
||||
// */
|
||||
// @Override
|
||||
// public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
|
||||
// HandlerExceptionResolver exceptionHandlerExceptionResolver = resolvers.stream().filter(x -> x instanceof ExceptionHandlerExceptionResolver).findAny().get();
|
||||
// int index = resolvers.indexOf(exceptionHandlerExceptionResolver);
|
||||
// resolvers.add(index, new CustomRuntimeResolver());
|
||||
// WebMvcConfigurer.super.extendHandlerExceptionResolvers(resolvers);
|
||||
// }
|
||||
|
||||
}
|
Loading…
Reference in New Issue