谷粒商城cors跨域访问问题处理

在按照教程配置了Gateway的CORS过滤器后,仍遇到跨域访问错误。通过搜索发现,将`corsConfiguration.addAllowedOrigin(*)`改为`corsConfiguration.addAllowedOriginPattern(*)`可以解决这个问题。修改后的代码展示了如何创建并注册一个允许所有来源的跨域配置。
摘要由CSDN通过智能技术生成

按教程getway配置了cors 过滤器,还是报跨域访问错误,百度查 ‘getway 配置CorsWebFilter 未生效’ ,发现需要修改一处地方,

corsConfiguration.addAllowedOrigin("*");
改成 corsConfiguration.addAllowedOriginPattern("*");

代码如下

public class GulimallCorsConfiguration {
    @Bean // 添加过滤器
    public CorsWebFilter corsWebFilter(){
        // 基于url跨域,选择reactive包下的
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        // 跨域配置信息
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许跨域的头
        corsConfiguration.addAllowedHeader("*");
        // 允许跨域的请求方式
        corsConfiguration.addAllowedMethod("*");
        // 允许跨域的请求来源
//        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedOriginPattern("*");//注释上面一行,添加这个配置
        // 是否允许携带cookie跨域
        corsConfiguration.setAllowCredentials(true);


        // 任意url都要进行跨域配置
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值