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

相关推荐
William一直在路上2 天前
KONG API Gateway中的核心概念
网络·gateway·kong
Java牛马5 天前
SpringCloud之Gateway
网关·spring cloud·gateway·路由·过滤器·断言
yh云想6 天前
《微服务SpringCloud架构实践指南:从Nacos到Gateway的全面解析》
spring cloud·nacos·gateway·openfeign·filter
飞火流星020279 天前
SkyWalking异步采集spring gateway日志
gateway·skywalking·日志监控·gateway链路监控
要开心吖ZSH9 天前
【Spring Cloud Gateway 实战系列】高级篇:服务网格集成、安全增强与全链路压测
spring cloud·微服务·gateway·istio
德育处主任Pro9 天前
解放生产力:Amazon API Gateway 与 Amazon Lambda 的优雅组合
gateway·aws·亚马逊
java叶新东老师9 天前
spring gateway 配置http和websocket路由转发规则
spring·http·gateway
java叶新东老师9 天前
五、搭建springCloudAlibaba2021.1版本分布式微服务-gateway网关
分布式·微服务·gateway
云和数据.ChenGuang10 天前
`neutron router-gateway-set` 操作失败的可能原因及解决方案
运维·gateway·运维技术总结·运维技术