java B2B2C源碼電子商務平臺 --zuul跨域訪問問題

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);
}

java B2B2C Springcloud仿淘寶電子商城系統

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章