Spring Cloud Gateway 跨域
https://docs.spring.io/spring-cloud-gateway/docs/2.2.6.RELEASE/reference/html/#cors-configuration
server: port: 8060 spring: application: name: api-gateway cloud: # gateway的配置 gateway: # 路由规则 routes: - id: order_route # 路由的唯一标识, 路由到 order # uri: http://localhost:8020 # 需要转发的地址 uri: lb://order-nacos-service # 需要转发的地址 lb:使用nacos中的本地负载均衡策略 # 断言规则 用于路由规则的匹配 predicates: - Path=/order-serv/** # http://localhost:8060/order-serv/order/add 路由转到 # http://localhost:8020/order-serv/order/add # - After=2017-01-20T17:42:47.789-07:00[Asia/Shanghai] # - Header=X-Request-Id,\d+ # - Method=GET # - Query=name,zhangsan|lisi # - CheckAuth=lisi filters: - StripPrefix=1 # 转发之前去掉第一层路径 # http://localhost:8020/order-serv/order/add 过虑成 # http://localhost:8020/order/add # - AddRequestHeader=X-Request-color, blue # - AddRequestParameter=color, red - PrefixPath=/mall-order #添加前缀, 对应微服务需要配置context-path # - RedirectTo=302, https://www.baidu.com/ #重定向到百度 # - SetStatus=404 # - CheckAuth=lisi #跨域配置 globalcors: cors-configurations: '[/**]': # 允许蹃域访问的资源 # allowedOrigins: "https://docs.spring.io" # 跨域允许来源 allowedOrigins: "*" # 跨域允许来源 allowedMethods: - GET - POST # 配置 Nacos nacos: server-addr: 127.0.0.1:8848 discovery: # server-addr: 127.0.0.1:8848 username: nacos password: nacos namespace: public
或
package com.wsm.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; @Configuration public class CorsConfig { public CorsWebFilter corsWebFilter() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedMethod("*"); // 允许的Method config.addAllowedOrigin("*"); // 允许的来源 config.addAllowedHeader("*"); // 允许的请求头参数 // 允许访问的资源 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); return new CorsWebFilter(source); } }