1.跨域:對於一個路徑:http:ip:port 如果協議 ip 端口三者有一個不同就有可能產生跨域問題
基於spring-cloud-gateway網關的解決方案,因為該網關使用的是reactor模式的webflux,所以:
@Configuration public class CommonConfig { @Bean public CorsWebFilter corsWebFilter() { return new CorsWebFilter(new CorsConfigurationSource() { @Override public CorsConfiguration getCorsConfiguration(ServerWebExchange serverWebExchange) { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true);//是否放行cookied corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);//所有的請求頭都允許 corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);//所有的請求方法GET POST等都允許跨域 corsConfiguration.addAllowedOrigin(CorsConfiguration.ALL);//所有的訪問來源都允許跨域 return corsConfiguration; } }); } }
如果是基於zuul作為網關,或者其他基於servlet的服務:
@Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowCredentials(true) .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .maxAge(3600); } }