springcloud微服務框架,是一組組件,eureka服務註冊中心,zuul路由等等
一般都是在zuul上配好url路徑映射到各個服務,所以對外都是訪問zuul服務的端口,但是在web服務設置了跨域的Interceptor後沒有起作用(我的chrome瀏覽器,postman正常),關掉web服務,依然有返回http
需要JAVA Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼
最後確定是在zuul上沒有設置跨域header
跨域時,可能會先OPTIONS訪問,zuul直接返回了,所以需要給zuul添加跨域header
import org.springframework.web.cors.UrlBasedCorsConfigurationSource
import org.springframework.web.cors.CorsConfiguration
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
List<String> origins = new ArrayList<>();
origins.add("*");
config.addAllowedOrigin(origins);
List<String> headers = new ArrayList<>();
headers.add("*");
config.addAllowedHeader(headers);
List<String> methods = new ArrayList<>();
methods.add("OPTIONS");
methods.add("GET");
methods.add("POST");
config.addAllowedMethod(methods);
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}