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

相关推荐
lwb_01181 天前
SpringCloud——Gateway新一代网关
spring·spring cloud·gateway
你是人间五月天1 天前
gateway断言配置详解
gateway
weixin_387545641 天前
深入解析 AI Gateway:新一代智能流量控制中枢
人工智能·gateway
yuluo_YX15 天前
AI Gateway 介绍
人工智能·gateway
网硕互联的小客服18 天前
502 Bad Gateway:服务器作为网关或代理时收到无效响应处理方式
运维·服务器·gateway
sevevty-seven18 天前
什么是Gateway
gateway
秋の花19 天前
【GateWay】和权限验证
java·gateway
欧先生^_^20 天前
org.springframework.cloud.gateway 组件解释
gateway
jarenyVO22 天前
Spring Cloud Gateway 全面学习指南
java·gateway
保持学习ing24 天前
微服务--Gateway网关
java·网关·微服务·gateway