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