码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【云原生&微服务>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.2spring-boot.version>
        <spring-cloud.version>2020.0.1spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1spring-cloud-alibaba.version>
    properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring-boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-cloud-alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    二、解决跨域问题

    在【云原生&微服务>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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

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

    在这里插入图片描述

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

  • 相关阅读:
    1397. 找到所有好字符串 KMP+数位dp
    [附源码]java毕业设计st音乐网站论文
    4年经验来面试25K的测试岗,连基础都不会,还不如招应届生。
    JSTL标准标签库 EL表达式
    【仿牛客网笔记】 Redis,一站式高性能存储方案——Redis入门
    server2012 通过防火墙开启局域网内限定IP进行远程桌面连接
    Scala语言入门
    短视频发布的黄金时间,四点两天,找准发布时机才能事半功倍
    力扣(226.112)补9.8
    SSH客户端连接远程服务器
  • 原文地址:https://blog.csdn.net/Saintmm/article/details/125837159
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号