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

相关推荐
ghostwritten18 小时前
云原生流量治理新标准:Kubernetes Gateway API 部署实践指南
云原生·kubernetes·gateway
齐 飞19 小时前
Spring Cloud Alibaba快速入门-Gateway
spring cloud·微服务·gateway
Linux运维技术栈19 小时前
Gravitee Kafka Gateway 规范部署:HTTP API化封装与安全隔离实践
http·kafka·gateway
trayvontang3 天前
Spring Gateway核心概念、流程及原理
spring·gateway·spring gateway
trayvontang3 天前
Spring Gateway常用过滤器(限流、熔断等)
spring·gateway·spring gateway·gateway常用过滤器
Hello.Reader5 天前
Flink SQL Gateway 把 Flink SQL 变成“多客户端并发可用”的统一服务入口
sql·flink·gateway
ygqygq27 天前
Kubernetes Gateway API 与 Envoy Gateway 部署使用指南
kubernetes·gateway·envoy·ingress
weixin_439706257 天前
spring boot+nacos+gateway+sentinel的简单例子
spring boot·gateway·sentinel
peihexian7 天前
ingress-nginx更换为f5 nginx gateway fabric
nginx·gateway·fabric
木子欢儿8 天前
阿里云系统磁盘总读BPS突然增长很高,导致网站502 Bad Gateway
阿里云·云计算·gateway