温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

zuul 跨域 springcloud

发布时间:2020-05-21 04:26:35 来源:网络 阅读:316 作者:fhspringcloud 栏目:编程语言

1.配置zuul
注意zuul要允许与进行跨域相关的头信息

zuul:
#需要忽略的头部信息,不在传播到其他服务
  sensitive-headers: Access-Control-Allow-Origin
  ignored-headers: Access-Control-Allow-Origin,H-APP-Id,Token,APPToken

2.注入CorsFilter

package com.cfh.practice.zuulserver.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;/**
 * @Author: fh www.1b23.com
 * @Date: 2019/9/24 20:36
 * @Description: 使用zuul解决请求跨域问题
 */@Configurationpublic class CorsConfig {    @Bean
    public CorsFilter corsFilter() {        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许cookies跨域
        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
        config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许
        source.registerCorsConfiguration("/**", config);        return new CorsFilter(source);
    }

}

这样我们就完成了跨域,但需要注意如果在最外层的服务网关配置了跨域,那服务自身就不能再进行跨域处理否则会引发冲突


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI