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

网友投稿 644 2022-09-02

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

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

文章目录

​​一、前言​​​​二、解决跨域问题​​

一、前言

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

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

聊了以下问题:

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

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

PS:SpringCloud版本信息:

2.4.2 2020.0.1 2021.1 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import

二、解决跨域问题

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

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

​​corsConfigurations​​属性对应一个Map结构:

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:源码深度剖析SpringBoot启动流程中开启OpenFeign的入口(ImportBeanDefinitionRegistrar详解)
下一篇:Thinkphp5微信小程序获取用户信息接口的实例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~