网站首页
Java
站长
开源
框架
理论
JS
Linux
DB
服务器
NET
生活
软件
PHP
其他
您的位置:首页 > 框架 > Spring Cloud Gateway 网关Cros跨域配置
Spring Cloud Gateway 网关Cros跨域配置
2022-2-7    2695    0

所有的微服务是通过网关这个入口,在和前端对接口时,必然设计到有关跨域的问题。关于服务端跨域有很多方案,可以加注解(指定具体的路径允许跨域),也可以统一配置。

另外如果不在网关入口这配置,势必会造成一个很大的影响,那就是前端通过网关入口调用其它微服务,通常会出现如下错误:


Access to XMLHttpRequest at 'xxx' from origin 'xxx' has been been blocked by CORS policy


官方说明文档

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#cors-configuration 

接口文档

https://docs.spring.io/spring-framework/docs/5.0.x/javadoc-api/org/springframework/web/cors/CorsConfiguration.html 


具体配置

server:
  port: 10010
spring:
  application:
    name: gatewayservice
  cloud:
    nacos:
      server-addr: 192.168.2.22:8848 # Nacos地址
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://www.javacui.com" # 允许那些网站跨域访问
            allowedMethods: "GET" # 允许那些Ajax方式的跨域请求
            allowedHeaders: "*" # 允许请求头携带信息
            allowCredentials: "*" # 允许携带cookie
            maxAge: 360000 # 这次跨域有效期于相同的跨域请求不会再预检


注意跨域预检会有一些性能影响,所以要注意maxAge这个参数。

上一篇: Windows上安装Seata服务
下一篇: Spring Cloud Gateway 全局过滤器
发表评论:
您的网名:
个人主页:
编辑内容: