package com.nmgs.config;

/**
 * 启用跨域配置
 * 编写SpringMVCConfig类使用FilterConfig中的配置
 *
 * @author Administrator
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@SuppressWarnings("deprecation")
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Resource
    private FilterConfig filterconfig;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 设置拦截类
        registry.addInterceptor(filterconfig)
                .addPathPatterns("/ServiceAreaOperators/**")
//                是否允许证书
                .excludePathPatterns(
                        "/MenuIndex/**",
                        "/ETCLanePay/**",
                        "/Manager/**",
                        "/logManager/**",
                        "/wxCarUserInfo/**",
                        "/showStanEcharts/**",
                        "/404/**",
                        "/MenuIndex/**",
                        "/index.html",
                        "/assets/**",
                        "/img/**",
                        "/static/**",
                        "/error/**",
                        "/webjars/**",
                        "/swagger-resources/**",
                        "/doc.html",
                        "/ServiceAreaOperators/updatePassword",
                        "/ServiceAreaOperators/addOperators"
                );


    }

    /**
     * 跨越配置
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 设置容许跨域请求的域名
        config.addAllowedOriginPattern("*");
//        config.addAllowedOrigin("*");
        // 设置容许的方法
        config.addAllowedMethod("*");
        // 容许任何头
        config.addAllowedHeader("*");
        config.addExposedHeader("token");
        //是否支持cookie跨域
        config.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

    /**
     * 重新指定静态资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决 SWAGGER2 404报错
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}