WebMvcConfig.java 4.8 KB
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.ClientInventoryServiceRpcClient;
import com.infoloop.tianting.service.client.MealOrderServiceRpcClient;
import com.infoloop.tianting.service.client.MeiZhongYiHeServiceClient;
import com.infoloop.tianting.service.client.OperatorServiceRpcClient;
import com.infoloop.tianting.service.client.WOperatorServiceRpcClient;
import com.infoloop.tianting.tracing.HttpTracingInterceptor;
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,
                        final MeiZhongYiHeServiceClient meiZhongYiHeServiceClient,
                        final MealOrderServiceRpcClient mealOrderServiceRpcClient,
                        final ClientInventoryServiceRpcClient clientInventoryServiceRpcClient) {
        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, meiZhongYiHeServiceClient, operatorServiceRpcClient, wOperatorServiceRpcClient));
        this.httpInterceptors.add(new ResourceInjector(mealOrderServiceRpcClient, clientInventoryServiceRpcClient));
        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("/**")
                .allowedOriginPatterns("*")
                .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;
    }
}