WebMvcConfig.java
4.22 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.infoloop.tianting.config;
import brave.Tracing;
import com.infoloop.tianting.filter.HttpServletRequestWrapperFilter;
import com.infoloop.tianting.intercepter.ApiSignInterceptor;
import com.infoloop.tianting.intercepter.LoginInterceptor;
import com.infoloop.tianting.intercepter.RequestMonitor;
import com.infoloop.tianting.intercepter.ResourceInjector;
import com.infoloop.tianting.service.client.ClientCustomerServiceRpcClient;
import com.infoloop.tianting.service.client.OperatorServiceRpcClient;
import com.infoloop.tianting.service.client.WOperatorServiceRpcClient;
import com.infoloop.tianting.tracing.HttpTracingInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
private final List<HandlerInterceptor> httpInterceptors;
@Autowired
public WebMvcConfig(final Tracing tracing,
final ResourceInjector resourceInjector,
final ClientCustomerServiceRpcClient clientCustomerServiceRpcClient,
final OperatorServiceRpcClient operatorServiceRpcClient,
final WOperatorServiceRpcClient wOperatorServiceRpcClient) {
this.httpInterceptors = new ArrayList<>();
this.httpInterceptors.add(new HttpTracingInterceptor(tracing));
this.httpInterceptors.add(new RequestMonitor());
this.httpInterceptors.add(new ApiSignInterceptor());
this.httpInterceptors.add(new LoginInterceptor(clientCustomerServiceRpcClient, operatorServiceRpcClient, wOperatorServiceRpcClient));
this.httpInterceptors.add(resourceInjector);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Override
public void addInterceptors(@NonNull InterceptorRegistry registry) {
for (final var interceptor : httpInterceptors) {
registry.addInterceptor(interceptor)
.addPathPatterns("/**")
.excludePathPatterns(SwaggerConfig.SWAGGER_WHITELIST);
}
}
@Override
public void addCorsMappings(CorsRegistry registry) {
// 跨域支持
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods(HttpMethod.GET.name(), HttpMethod.POST.name(), HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name(), HttpMethod.OPTIONS.name())
.allowCredentials(true);
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix("api", c -> c.getPackage().getName().contains("com.infoloop.tianting.controller") || c.getPackage().getName().contains("com.infoloop.tianting.streamController"));
}
@Bean
public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilter() {
final var registrationBean = new FilterRegistrationBean<HttpServletRequestWrapperFilter>();
registrationBean.setFilter(new HttpServletRequestWrapperFilter());
registrationBean.addUrlPatterns("/*");
registrationBean.addUrlPatterns();
return registrationBean;
}
}