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

相关推荐
江畔何人初7 小时前
Gateway API 的核心组件与作用
运维·网络·云原生·kubernetes·gateway
zklgin8 小时前
Gateway Timeout504 网关超时的完美解决方法
gateway
WwW.-.2 天前
OpenClaw 技术解析:多渠道 AI Gateway 如何连接消息、Agent 与远程节点
网络·人工智能·gateway
@小匠2 天前
Spring-Gateway-理论知识总结/常问面试题
数据库·spring·gateway
胡斌附体2 天前
OpenClaw Gateway 代理问题排查记录
gateway·proxy·openclaw
大傻^3 天前
【OpenClaw -04】OpenClaw Gateway 架构:单一控制平面与 Agent 运行时模型
gateway·mcp·openclaw
坐吃山猪16 天前
OpenClaw04_Gateway常见问题
网络·gateway·openclaw
三水不滴16 天前
利用SpringCloud Gateway 重试 + 降级解决第三方接口频繁超时问题,提升性能
经验分享·笔记·后端·spring·spring cloud·gateway
知识即是力量ol17 天前
微服务架构:从入门到进阶完全指南
java·spring cloud·微服务·nacos·架构·gateway·feign
j2001032217 天前
Gateway—— 高级流量路由
gateway·k8s