package com.supplier.config; import com.supplier.util.JWTIntercept; 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.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class InterceptConfig implements WebMvcConfigurer { /** * 重写父类提供的跨域请求处理的接口 * * @param registry */ @Override public void addCorsMappings(CorsRegistry registry) { // 添加映射路径 registry.addMapping("/**") // 放行哪些原始域 .allowedOriginPatterns("*") // 是否发送Cookie信息 .allowCredentials(true) // 放行哪些原始域(请求方式) .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "HEAD") // 放行哪些原始域(头部信息) .allowedHeaders("*") // 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息) .exposedHeaders("Server", "Content-Length", "Authorization", "Access-Token", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials"); } private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址 corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头 corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法 return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置 return new CorsFilter(source); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new JWTIntercept()) .addPathPatterns("/**") // 正常情况下 拦截所有请求/**,因测试需要就先拦截/user/test .excludePathPatterns("/", "/wx/login/**", "/page/*", "/js/**", "/i18n/**", "/css/**", "/images/**", "/layui/**", "/json/**" ,"/remixicon/**", "/wxpay/**","/appPay/**", "http://a1-v2.easemob.com/**", "http://39.97.9.52/*", "http://47.95.246.247/**", "ws://39.102.159.61/**", "/page/registered/**"); //放心登录接口,因为要通过登录获取token } }