解决网关gateway跨域问题

@Configuration
public class CorsConfig {
    /**
     * 跨域配置
     * @return
     */
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        corsConfiguration.setMaxAge(18000L);
        // 允许cookies跨域
        corsConfiguration.setAllowCredentials(true);
        // 允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        corsConfiguration.addAllowedOrigin("*");
        // 允许访问的头信息,*表示全部
        corsConfiguration.addAllowedHeader("*");
        // 允许提交请求的方法类型,*表示全部允许
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    /**
     * 自定义跨域过滤器
     * @return
     */
    @Bean
    public CorsWebFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsWebFilter(source);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Gateway网关中进行跨域配置的方法是通过在application.yml文件中进行配置。具体的配置内容如下所示: ```yaml server: port: 10011 spring: application: name: gateway cloud: nacos: server-addr: 127.0.0.1:8848 gateway: routes: - id: user-service uri: lb://userservice predicates: - Path=/user/** - id: admin-service uri: lb://adminservice predicates: - Path=/admin/** globalcors: add-to-simple-url-handler-mapping: true cors-configurations: '\[/**\]': allowedOriginPatterns: "*" allowedHeaders: "*" allowedMethods: - "GET" - "POST" - "DELETE" - "PUT" - "OPTIONS" allowCredentials: true maxAge: 3600 ``` 这段配置代码中,通过设置`globalcors`来实现跨域配置。`allowedOriginPatterns`表示允许跨域的来源,使用通配符`*`表示允许所有来源。`allowedHeaders`表示允许跨域的请求头,同样使用通配符`*`表示允许所有请求头。`allowedMethods`表示允许跨域的请求方法,包括GET、POST、DELETE、PUT和OPTIONS。`allowCredentials`表示是否允许发送Cookie等凭证信息。`maxAge`表示本次跨域检测的有效期,单位为秒。 需要注意的是,以上配置是针对整个网关跨域配置,如果需要对某个具体的路由进行跨域配置,可以在对应的路由配置中添加`filters`来实现。 #### 引用[.reference_title] - *1* [Gateway网关配置跨域](https://blog.csdn.net/weixin_62166514/article/details/131287227)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringCloud笔记(五)——Gateway](https://blog.csdn.net/weixin_46508271/article/details/121754796)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值