springcloud整合网关(springcloud-gateway) 跨域处理

pom引入依赖

cpp 复制代码
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!-- 服务注册 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

application.yml文件内容

cpp 复制代码
server:
  port: 8099
spring:
  application:
    name: serviceGateway
  # nacos服务地址
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      #使用服务发现路由
      discovery:
        locator:
          enabled: true
      routes:
        - id: service-hosp #设置路由id  #设置路由断言,代理servicerId为auth-service的/auth/路径
          uri: lb://serviceHosp #设置路由的uri
          predicates:
            - Path=/*/hosp/**

        - id: service-cmn 
          uri: lb://service-cmn
          predicates:
            - Path=/*/cmn/**

处理跨越问题

cpp 复制代码
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }
}

前端访问后端端口直接配置为网关的端口,路径不变

nacos服务列表为

相关推荐
stormsha1 小时前
Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统
服务器·网络·网络安全·gateway
背太阳的牧羊人10 小时前
backend 服务尝试连接 qdrant 容器,但失败了,返回 502 Bad Gateway 问题排查
docker·gateway·qdrant
曼彻斯特的海边1 天前
RequestRateLimiterGatewayFilterFactory
spring cloud·gateway·限流
·云扬·1 天前
【PmHub面试篇】Gateway全局过滤器统计接口调用耗时面试要点解析
面试·职场和发展·gateway
devil_mf1 天前
gateway 网关 路由新增 (已亲测)
gateway
wmd131643067121 天前
Gateway 搭建
gateway
KubeSphere 云原生3 天前
云原生周刊:探索 Gateway API v1.3.0
云原生·gateway
曼彻斯特的海边4 天前
spring-cloud-alibaba-sentinel-gateway
gateway·sentinel·springcloud
xujinwei_gingko6 天前
网关Gateway
微服务·gateway
徐子童6 天前
《Spring Cloud Gateway 快速入门:从路由到自定义 Filter 的完整教程》
java·开发语言·spring cloud·nacos·gateway