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

相关推荐
运维开发王义杰2 天前
Kubernetes:EKS 中 Istio Ingress Gateway 负载均衡器配置及常见问题解析
kubernetes·gateway·istio
铭毅天下3 天前
极限网关 INFINI Gateway 从 0 到 1:简单易懂的入门教程
gateway
铭毅天下3 天前
极限网关核心架构解析:从 Nginx 到 INFINI Gateway 的演进
运维·nginx·架构·gateway
Xwzzz_4 天前
Spring Cloud Gateway中断言路由和过滤器的使用
java·spring cloud·gateway
zzyh1234564 天前
springcloudalibaba组件gateway
前端·javascript·gateway
@明明不知道4 天前
spring cloud 微服务部署(2025年)第一章:Nacos、LoadBalancer、GateWay、Ribbon集成之Nacos部署
spring cloud·微服务·nacos·gateway·loadbalancer
山海不说话5 天前
从零搭建微服务项目(第7章——微服务网关模块基础实现)
java·spring boot·spring·spring cloud·微服务·gateway
默辨5 天前
再谈SpringCloud Gateway源码
java·网关·spring cloud·gateway·reactor·webflux
飞火流星020276 天前
【动态路由】系统web url整合系列【springcloud-gateway实现】【不改hosts文件版】组件一:多个Eureka路由过滤器
gateway·gateway动态路由·gateway实现反向代理·系统url整合·eureka路由过滤器·gateway路由过滤器
azoon.top6 天前
springcloud集成gateway
微服务·gateway·springcloud