springcloud html跨域訪問

springcloud html跨域訪問

錯誤提示:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/user-server/login. (Reason: CORS request did not succeed).
[詳細瞭解]:已阻止跨源請求:同一源策略不允許讀取位於http://localhost:8080/user-server/login的遠程資源。(原因:CORS請求沒有成功)。[詳細瞭解]

解決方案,在zuul網關加入以下配置,注意需要被spring容器掃描到:

      /**
     * @description: TODO
     * @param []
     * @return org.springframework.web.filter.CorsFilter
     * @author 丁乾文 
     * @date 19-5-5 上午11:15 
     */
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        // 允許cookies跨域
        config.setAllowCredentials(true);
        // #允許向該服務器提交請求的URI,*表示全部允許,在SpringMVC中,如果設成*,會自動轉成當前請求頭中的Origin
        config.addAllowedOrigin("*");
        // #允許訪問的頭信息,*表示全部
        config.addAllowedHeader("*");
        // 預檢請求的緩存時間(秒),即在這個時間段裏,對於相同的跨域請求不會再預檢了
        config.setMaxAge(18000L);
        // 允許提交請求的方法,*表示全部允許
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        // 允許Get的請求方法
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章