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

相关推荐
standovon3 小时前
Gateway Timeout504 网关超时的完美解决方法
gateway
无名-CODING4 小时前
SpringCloud 网关与熔断:Gateway + Sentinel 快速入门
spring cloud·gateway·sentinel
会飞的大可8 小时前
022 API网关设计:Gateway路由、限流与鉴权实战
gateway
晏宁科技YaningAI2 天前
全球短信路由系统设计逻辑打破 80%送达率瓶颈:工程实践拆解
网络·网络协议·架构·gateway·信息与通信·paas
ꟼ ꟼ✚19226382 天前
基于 FPGA 的 16QAM 调制解调系统功能说明文档
gateway
code_pgf3 天前
openclaw配置高德导航、京东商品搜索、QQ 音乐播放控制
人工智能·gateway·边缘计算
码克疯v14 天前
OpenClaw 安装与入门:从零到跑通 Gateway(详细可操作)
gateway·openclaw·龙虾
qqty12174 天前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
北巷`4 天前
OpenClaw内部原理完全解析:从Gateway到记忆系统的AI Agent基础设施
人工智能·gateway
yuweiade4 天前
SpringGateway网关(Spring Gateway是Spring自己编写的,也是SpringCloud中的组件)
spring·spring cloud·gateway