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

相关推荐
成为你的宁宁3 小时前
【运维读懂 Spring Cloud Gateway】
gateway·api网关
Raas1001 天前
MAI Gateway(魔芋企业级AI网关):企业AI网关的合规审计与调用留痕设计
大数据·人工智能·gateway·企业·api网关·mai gateway·企业级网关
Sammyyyyy1 天前
AI Gateway 与直连 LLM API 的应该怎么选,一篇文章说明白
人工智能·ai·gateway·ai编程·ai-native·servbay
snow@li1 天前
Gateway网关全景深度分析:外部请求进入微服务全链路解析
gateway
Raas1002 天前
MAI Gateway(魔芋企业级AI网关):私有化部署的身份同步与数据内控设计
网关·网络安全·大模型·gateway·mai gateway·企业级网关
aFakeProgramer8 天前
S‑CORE someip‑gateway 上手文档
gateway
智码看视界9 天前
Day46—Spring Cloud Gateway:路由/过滤器/限流全攻略
java·gateway·限流·微服务网关·过滤器链
wangjialelele11 天前
Spring Cloud 全体系学习笔记(REST、Nacos、Feign、Gateway)
java·spring boot·笔记·学习·spring·spring cloud·gateway