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

相关推荐
一个儒雅随和的男子19 小时前
Spring cloud组件gateway网关详细剖析
spring·spring cloud·gateway
超梦dasgg21 小时前
Gateway 鉴权场景:网关统一鉴权 + 业务应用决定放行规则
java·gateway
木雷坞1 天前
AI Gateway 接入大模型服务后首 token 慢排查:镜像、模型缓存和 GPU 节点
人工智能·缓存·gateway
明夜之约2 天前
Spring Cloud Gateway 深度解析:从路由原理到生产级网关实战
java·spring·spring cloud·gateway
tyler_download3 天前
深入深出openclaw:gateway 服务进程的启动逻辑
gateway
Upsy-Daisy3 天前
OpenClaw 源码解析(七):Gateway 控制平面与 WebSocket RPC 机制
websocket·平面·gateway
香菜烤面包3 天前
SGLang Model Gateway 核心功能解析
gateway
a752066286 天前
Windows 11运行OpenClaw(小龙虾)完整指南:从下载到Gateway在线
人工智能·windows·gateway·小龙虾·ai 办公自动化·小龙虾一键部署
阿里-于怀8 天前
告别 Ingress Nginx:云原生 API 网关 Gateway API 使用指引
nginx·云原生·gateway
comcoo9 天前
OpenClaw AI 聊天网关配置教程|Gateway 启动与完整使用指南
运维·人工智能·elasticsearch·gateway·openclaw安装包·open claw部署