(转载)Spring Cloud Gateway配置跨域,实测有效

注:本文虽为转载文章,但代码和一些文字稍有不同
原文声明:

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_40623736/article/details/110665826

gateway配置

spring:
  cloud:
    gateway:      
      globalcors:
        cors-configurations:
          '[/**]':
            allowCredentials: true
            allowedOrigins: "*"
            allowedMethods: "*"
            allowedHeaders: "*"        	

网上搜了一堆,大多都是上面的这些,但是如果只进行这样的配置的话,根本不会生效,我这边的gateway是集成了oauth2进行鉴权的,最后在Spring Security官方文档上看到了这一段
https://docs.spring.io/spring-security/site/docs/current/reference/html5/#cors
在这里插入图片描述

琢磨一番加参考其他博客之后。。。

gateway添加cros跨域配置文件

CorsConfig.java

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.config.GlobalCorsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;


@Configuration
@EnableConfigurationProperties(GlobalCorsProperties.class)
public class CorsConfig {

    @Order(Ordered.HIGHEST_PRECEDENCE)
    @RefreshScope
    @Bean
    public CorsWebFilter corsWebFilter(GlobalCorsProperties globalCorsProperties){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        globalCorsProperties.getCorsConfigurations().forEach(source::registerCorsConfiguration);
        return new CorsWebFilter(source);
    }
}

本人实测原文的代码跨域也是有效

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Spring Cloud Gateway配置跨域,可以使用 Spring WebFlux 提供的 CorsConfigurationSource 类。具体步骤如下: 1. 创建一个 CorsConfiguration 对象,配置跨域的属性,比如允许的域名、允许的请求方法等。 ``` CorsConfiguration corsConfig = new CorsConfiguration(); corsConfig.addAllowedOrigin("*"); corsConfig.addAllowedMethod("*"); corsConfig.addAllowedHeader("*"); ``` 2. 创建一个 CorsConfigurationSource 对象,将 CorsConfiguration 对象传入其中。 ``` UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfig); ``` 3. 在 Spring Cloud Gateway配置类中将 CorsConfigurationSource 对象添加到路由过滤器中。 ``` @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path("/api/**") .filters(f -> f.cors(corsConfigSource)) // 添加跨域过滤器 .uri("lb://service")) .build(); } @Bean public CorsConfigurationSource corsConfigSource() { CorsConfiguration corsConfig = new CorsConfiguration(); corsConfig.addAllowedOrigin("*"); corsConfig.addAllowedMethod("*"); corsConfig.addAllowedHeader("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfig); return source; } ``` 这样就可以在 Spring Cloud Gateway配置跨域了。需要注意的是,由于 Spring Cloud Gateway 是基于 Spring WebFlux 开发的,因此跨域配置方式与传统的 Spring MVC 不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值