您当前的位置: 首页 >  gateway

陈橙橙丶

暂无认证

  • 3浏览

    0关注

    107博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringCloud gateway统一配置跨域

陈橙橙丶 发布时间:2020-05-30 13:34:51 ,浏览量:3

场景:

在这里插入图片描述

在这里插入图片描述

网关配置: 在这里插入图片描述

并没有做跨域处理。 如何解决,当然我们不应该对每一个请求单独的处理跨域,数量大多,我们只需要在网关模块下声明一个跨域配置即可

@Configuration
public class MyCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1.配置跨域
        //允许哪种请求头跨域
        corsConfiguration.addAllowedHeader("*");
        //允许哪种方法类型跨域 get post delete put
        corsConfiguration.addAllowedMethod("*");
        // 允许哪些请求源跨域
        corsConfiguration.addAllowedOrigin("*");
        // 是否携带cookie跨域
        corsConfiguration.setAllowCredentials(true);

        //允许跨域的路径
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}

重启网关服务

在这里插入图片描述

就解决了跨域。解决跨域的方式有很多,比如Nginx代理也能处理。

关注
打赏
1648473527
查看更多评论
立即登录/注册

微信扫码登录

0.0358s