【云原生&微服务>SCG网关篇十一】Spring Cloud Gateway解决跨域问题

一、前言

至此微服务网关系列文章已出:

  1. 【云原生&微服务>SCG网关篇一】为什么要有网关、生产环境如何选择网关
  2. 云原生&微服务>SCG网关篇二】生产上那些灰度发布方式
  3. 【云原生&微服务>SCG网关篇三】Spring Cloud Gateway是什么、详细使用案例
  4. 云原生&微服务>SCG网关篇四】Spring Cloud Gateway内置的11种PredicateFactory如何使用
  5. 【云原生&微服务>SCG网关篇五】Spring Cloud Gateway自定义PredicateFactory
  6. 【云原生&微服务>SCG网关篇六】Spring Cloud Gateway内置的18种Filter使用姿势
  7. 【云原生&微服务>SCG网关篇七】Spring Cloud Gateway基于内置Filter实现限流、熔断、重试
  8. 【云原生&微服务>SCG网关篇八】Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式
  9. 【云原生&微服务>SCG网关篇九】Spring Cloud Gateway集成Nacos详细案例
  10. 【云原生&微服务>SCG网关篇十】Spring Cloud Gateway集成Actuator、Zipkin详细案例

聊了以下问题:

  1. 为什么要有网关?网关的作用是什么?
  2. 网关的分类?
  3. 网关的技术选型?
  4. 使用网关时常用的灰度发布方式有哪些?
  5. Spring Cloud Gateway是什么?详细使用案例?
  6. Spring Cloud Gateway内置的11种PredicateFactory
  7. 如何自定义PredicateFactory?
  8. Spring Cloud Gateway内置的18种常用的Filter
  9. Spring Cloud Gateway基于内置Filter实现限流、熔断、重试
  10. Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式
  11. Spring Cloud Gateway集成Nacos案例
  12. Spring Cloud Gateway集成Actuator、Zipkin案例

本文接着聊Spring Cloud Gateway如何解决CORS跨域问题;

PS:SpringCloud版本信息:

<properties>
    <spring-boot.version>2.4.2</spring-boot.version>
    <spring-cloud.version>2020.0.1</spring-cloud.version>
    <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--整合spring cloud-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--整合spring cloud alibaba-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

二、解决跨域问题

【云原生&微服务>SCG网关篇一】为什么要有网关、生产环境如何选择网关一文我们聊到网关可以解决跨域问题,这里我们来看一下Spring Cloud Gateway是如何解决跨域问题的。

参考官方文章:https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#cors-configuration

通过spring.cloud.gateway.globalcors.corsConfigurations来处理CORS;

spring:
  cloud:
    gateway:
      # 解决跨域问题
      globalcors:
        corsConfigurations:
          '[/**]': # 匹配所有请求
            # 设置允许的域名
            allowedOrigins:
              - "http://localhost:18003"
            # 允许所有头信息
            allowedHeaders: "*"
            # 设置允许携带cookie
            # 为true时allowedOrigins不允许为* 会报错
            allowCredentials: true
            allowedMethods: # 支持的方法
              - GET
              - POST
              - PUT
              - DELETE

corsConfigurations属性对应一个Map结构:
在这里插入图片描述

其中,示例中的[/**]作为Map的一个key,表示匹配所有请求,而请求相关的CORS配置信息均体现在CorsConfiguration类中:

扫描二维码关注公众号,回复: 14379930 查看本文章

在这里插入图片描述

特别注意:当设置allowCredentials参数为 true 时,allowedOrigins不允许为 *,否则会报错!

猜你喜欢

转载自blog.csdn.net/Saintmm/article/details/125837159