前后端分离-服务端解决跨域问题引入网关之坑。

如果采用服务端解决跨域问题,一般做法是在application类中的filter中添加如下代码:

@Bean
public CorsFilter corsFilter() {
   final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
   final CorsConfiguration config = new CorsConfiguration();
   config.setAllowCredentials(true);
   config.addAllowedOrigin("*");
   config.addAllowedHeader("*");
   config.addAllowedMethod("OPTIONS");
   config.addAllowedMethod("HEAD");
   config.addAllowedMethod("GET");
   config.addAllowedMethod("PUT");
   config.addAllowedMethod("POST");
   config.addAllowedMethod("DELETE");
   config.addAllowedMethod("PATCH");
   source.registerCorsConfiguration("/**", config);
   return new CorsFilter(source);
}

那如果引入GetWay网关,并且在网关的filter中也假如解决跨域问题代码,那么在http服务请求时就会报如下问题:

    The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. 

查看http请求发现,response header 中的参数均是返回两遍。如下:



导致此现象的问题是我们web请求是通过GetWay代理转发,这样网关和api服务均解决跨域问题,导致重复返回。因此,如果服务是通过网关代理那么服务里就不要添加跨域问题代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值