使用Gateway解决跨域问题时配置文件不生效的情况之一

首先html文件只有一个发送ajax请求

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试</title>
</head>
<body>
</body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.get("http://localhost:10010/user/1?authorize=admin")
    .then(res=> console.log(res.data))
    .catch(err=>console.log(err))
</script>
</html>

而gateway的配置文件如下

java 复制代码
spring:
  application:
    name: gateway
  cloud:
    gateway:
      globalcors:
        add-to-simple-url-handler-mapping: true
        cors-configurations:
          '[/**]':
            allowedOrigins: 
              - "http://localhost:8090"
              - "http://www.域名.com"
            allowedMethods:
              - "GET"
              - "POST"
              - "DELETE"
              - "PUT"
              - "OPTIONS"
            allowedHeaders: "*" 
            allowCredentials: true
            maxAge: 360000

使用VSCode打开html页面时,发送ajax请求后浏览器控制台打印结果如下

显而易见,html发送的请求没有与gateway设置的允许跨域的url地址匹配上,而将配置文件中的localhost修改为127.0.0.1时,再次访问就可以成功响应。

如果想要localhost也可以正常响应的话,需要修改C:\Windows\System32\drivers\etc\hosts文件添加如下信息

127.0.0.1 localhost

相关推荐
dinga198510261 天前
当遇到 502 错误(Bad Gateway)怎么办
gateway
岳来1 天前
网络小白对容器参数endpoint 和gateway 对比
网络·docker·容器·gateway·endpoint
invicinble1 天前
分布式组件的全域认识和操作--gateway
分布式·gateway
阿里-于怀1 天前
Higress 已支持全新 Gateway API / Inference Extension
gateway·higress·ingress nginx
猫头虎2 天前
猫头虎AI分享:[转载]2025 年 HAMi 社区年度回顾 | 从 GPU 调度器到云原生 AI 基础设施的中流砥柱
运维·人工智能·云原生·开源·gateway·github·ai编程
文静小土豆5 天前
K8s Gateway API 全面指南:从入门到实践
kubernetes·gateway
码农垦荒笔记5 天前
OpenClaw实战#05-2:第二层工程拆解 Gateway 深度解析
人工智能·语言模型·gateway·agent·openclaw
星河耀银海6 天前
微服务网关设计与实战:Spring Cloud Gateway全解析
java·spring·微服务·云原生·架构·gateway
dinga198510266 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
ZeroNews内网穿透6 天前
通过 ZeroNews 远程管理 OpenClaw GateWay Dashboard
运维·服务器·数据库·gateway·ssh·clawdbot