码农知识堂 - 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不允许为 *,否则会报错!

  • 相关阅读:
    Java之组合模式
    【无标题】CTreeCtrl更改-/+展开按钮颜色
    Python轻量级Web框架:Bottle库
    语法基础(函数)
    参数解析(牛客)
    Docker入门Dockerfile详解及镜像创建
    Autosar MCAL-ADC详解(二)-基于Tc27x的cfg软件
    java 身份证号码验证
    【每日一题Day352】LC1726同积元组 | 哈希表+排列组合
    为什么每个人都在谈论Art Gobblers
  • 原文地址:https://blog.csdn.net/Saintmm/article/details/125837159
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号