提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.apilog.config; |
H |
2 |
|
|
3 |
import com.iailab.framework.apilog.core.filter.ApiAccessLogFilter; |
|
4 |
import com.iailab.framework.apilog.core.interceptor.ApiAccessLogInterceptor; |
|
5 |
import com.iailab.framework.apilog.core.service.ApiAccessLogFrameworkService; |
|
6 |
import com.iailab.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl; |
|
7 |
import com.iailab.framework.apilog.core.service.ApiErrorLogFrameworkService; |
|
8 |
import com.iailab.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl; |
|
9 |
import com.iailab.framework.common.enums.WebFilterOrderEnum; |
|
10 |
import com.iailab.framework.web.config.WebProperties; |
|
11 |
import com.iailab.framework.web.config.IailabWebAutoConfiguration; |
|
12 |
import com.iailab.module.infra.api.logger.ApiAccessLogApi; |
|
13 |
import com.iailab.module.infra.api.logger.ApiErrorLogApi; |
|
14 |
import org.springframework.beans.factory.annotation.Value; |
|
15 |
import org.springframework.boot.autoconfigure.AutoConfiguration; |
|
16 |
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|
17 |
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
|
18 |
import org.springframework.context.annotation.Bean; |
|
19 |
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
|
20 |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
21 |
|
|
22 |
import javax.servlet.Filter; |
|
23 |
|
|
24 |
@AutoConfiguration(after = IailabWebAutoConfiguration.class) |
|
25 |
public class IailabApiLogAutoConfiguration implements WebMvcConfigurer { |
|
26 |
|
|
27 |
@Bean |
|
28 |
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") |
|
29 |
public ApiAccessLogFrameworkService apiAccessLogFrameworkService(ApiAccessLogApi apiAccessLogApi) { |
|
30 |
return new ApiAccessLogFrameworkServiceImpl(apiAccessLogApi); |
|
31 |
} |
|
32 |
|
|
33 |
@Bean |
|
34 |
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") |
|
35 |
public ApiErrorLogFrameworkService apiErrorLogFrameworkService(ApiErrorLogApi apiErrorLogApi) { |
|
36 |
return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi); |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* 创建 ApiAccessLogFilter Bean,记录 API 请求日志 |
|
41 |
*/ |
|
42 |
@Bean |
|
43 |
@ConditionalOnProperty(prefix = "iailab.access-log", value = "enable", matchIfMissing = true) // 允许使用 iailab.access-log.enable=false 禁用访问日志 |
|
44 |
public FilterRegistrationBean<ApiAccessLogFilter> apiAccessLogFilter(WebProperties webProperties, |
|
45 |
@Value("${spring.application.name}") String applicationName, |
|
46 |
ApiAccessLogFrameworkService apiAccessLogFrameworkService) { |
|
47 |
ApiAccessLogFilter filter = new ApiAccessLogFilter(webProperties, applicationName, apiAccessLogFrameworkService); |
|
48 |
return createFilterBean(filter, WebFilterOrderEnum.API_ACCESS_LOG_FILTER); |
|
49 |
} |
|
50 |
|
|
51 |
private static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) { |
|
52 |
FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter); |
|
53 |
bean.setOrder(order); |
|
54 |
return bean; |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public void addInterceptors(InterceptorRegistry registry) { |
|
59 |
registry.addInterceptor(new ApiAccessLogInterceptor()); |
|
60 |
} |
|
61 |
|
|
62 |
} |