dongyukun
9 天以前 5f653cd83425db29ccf514e9fd966a90c7eeab32
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.web.config;
H 2
3 import com.iailab.framework.apilog.core.service.ApiErrorLogFrameworkService;
4 import com.iailab.framework.common.enums.WebFilterOrderEnum;
5 import com.iailab.framework.web.core.filter.CacheRequestBodyFilter;
6 import com.iailab.framework.web.core.filter.DemoFilter;
7 import com.iailab.framework.web.core.handler.GlobalExceptionHandler;
8 import com.iailab.framework.web.core.handler.GlobalResponseBodyHandler;
9 import com.iailab.framework.web.core.util.WebFrameworkUtils;
10 import org.springframework.beans.factory.annotation.Value;
11 import org.springframework.boot.autoconfigure.AutoConfiguration;
12 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
13 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
14 import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
15 import org.springframework.boot.context.properties.EnableConfigurationProperties;
16 import org.springframework.boot.web.client.RestTemplateBuilder;
17 import org.springframework.boot.web.servlet.FilterRegistrationBean;
18 import org.springframework.context.annotation.Bean;
19 import org.springframework.util.AntPathMatcher;
20 import org.springframework.web.bind.annotation.RestController;
21 import org.springframework.web.client.RestTemplate;
22 import org.springframework.web.cors.CorsConfiguration;
23 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
24 import org.springframework.web.filter.CorsFilter;
25 import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
26 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
27
28 import javax.annotation.Resource;
29 import javax.servlet.Filter;
30
31 @AutoConfiguration
32 @EnableConfigurationProperties(WebProperties.class)
33 public class IailabWebAutoConfiguration implements WebMvcConfigurer {
34
35     @Resource
36     private WebProperties webProperties;
37     /**
38      * 应用名
39      */
40     @Value("${spring.application.name}")
41     private String applicationName;
42
43     @Override
44     public void configurePathMatch(PathMatchConfigurer configurer) {
45         configurePathMatch(configurer, webProperties.getAdminApi());
46         configurePathMatch(configurer, webProperties.getAppApi());
47     }
48
49     /**
50      * 设置 API 前缀,仅仅匹配 controller 包下的
51      *
52      * @param configurer 配置
53      * @param api        API 配置
54      */
55     private void configurePathMatch(PathMatchConfigurer configurer, WebProperties.Api api) {
56         AntPathMatcher antPathMatcher = new AntPathMatcher(".");
57         configurer.addPathPrefix(api.getPrefix(), clazz -> clazz.isAnnotationPresent(RestController.class)
58                 && antPathMatcher.match(api.getController(), clazz.getPackage().getName())); // 仅仅匹配 controller 包
59     }
60
61     @Bean
62     public GlobalExceptionHandler globalExceptionHandler(ApiErrorLogFrameworkService ApiErrorLogFrameworkService) {
63         return new GlobalExceptionHandler(applicationName, ApiErrorLogFrameworkService);
64     }
65
66     @Bean
67     public GlobalResponseBodyHandler globalResponseBodyHandler() {
68         return new GlobalResponseBodyHandler();
69     }
70
71     @Bean
72     @SuppressWarnings("InstantiationOfUtilityClass")
73     public WebFrameworkUtils webFrameworkUtils(WebProperties webProperties) {
74         // 由于 WebFrameworkUtils 需要使用到 webProperties 属性,所以注册为一个 Bean
75         return new WebFrameworkUtils(webProperties);
76     }
77
78     // ========== Filter 相关 ==========
79
80     /**
81      * 创建 CorsFilter Bean,解决跨域问题
82      */
83     @Bean
84     public FilterRegistrationBean<CorsFilter> corsFilterBean() {
85         // 创建 CorsConfiguration 对象
86         CorsConfiguration config = new CorsConfiguration();
87         config.setAllowCredentials(true);
88         config.addAllowedOriginPattern("*"); // 设置访问源地址
89         config.addAllowedHeader("*"); // 设置访问源请求头
90         config.addAllowedMethod("*"); // 设置访问源请求方法
91         // 创建 UrlBasedCorsConfigurationSource 对象
92         UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
93         source.registerCorsConfiguration("/**", config); // 对接口配置跨域设置
94         return createFilterBean(new CorsFilter(source), WebFilterOrderEnum.CORS_FILTER);
95     }
96
97     /**
98      * 创建 RequestBodyCacheFilter Bean,可重复读取请求内容
99      */
100     @Bean
101     public FilterRegistrationBean<CacheRequestBodyFilter> requestBodyCacheFilter() {
102         return createFilterBean(new CacheRequestBodyFilter(), WebFilterOrderEnum.REQUEST_BODY_CACHE_FILTER);
103     }
104
105     /**
106      * 创建 DemoFilter Bean,演示模式
107      */
108     @Bean
109     @ConditionalOnProperty(value = "iailab.demo", havingValue = "true")
110     public FilterRegistrationBean<DemoFilter> demoFilter() {
111         return createFilterBean(new DemoFilter(), WebFilterOrderEnum.DEMO_FILTER);
112     }
113
114     public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
115         FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
116         bean.setOrder(order);
117         return bean;
118     }
119
120     /**
121      * 创建 RestTemplate 实例
122      *
123      * @param restTemplateBuilder {@link RestTemplateAutoConfiguration#restTemplateBuilder}
124      */
125     @Bean
126     @ConditionalOnMissingBean
127     public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
128         return restTemplateBuilder.build();
129     }
130
131 }