使用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

相关推荐
MadPrinter12 小时前
OpenClaw Gateway 卡死假死问题完整诊断与预防方案
gateway
RemainderTime18 小时前
(十一)Spring Cloud Alibaba 2023.x:构建分布式全链路日志追踪体系
分布式·微服务·架构·gateway
qingwufeiyang_53018 小时前
统一网关GateWay
linux·服务器·gateway
dgvri2 天前
Gateway Timeout504 网关超时的完美解决方法
gateway
回到原点的码农2 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
小川zs3 天前
OpenClaw Gateway 频繁断开/重启问题诊断
linux·服务器·gateway
没有bug.的程序员3 天前
黑客僵尸网络的降维打击:Spring Cloud Gateway 自定义限流剿杀 Sentinel 内存黑洞
java·网络·spring·gateway·sentinel
zhangshuang-peta3 天前
弥合 n8n 中的 AI 上下文鸿沟:为何采用 MCP Gateway 构建更智能的工作流
网络·人工智能·gateway·ai agent·mcp·peta
shamalee5 天前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
xiaolingting5 天前
Gateway 网关流控与限流架构指南
spring cloud·架构·gateway·sentinel