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服务列表为

相关推荐
岳来9 小时前
网络小白docker network create时如何指定subnet 和gateway
网络·docker·gateway·subnet
如来神掌十八式1 天前
nginx + spring gateway+spring 服务_nginx 转发到 gateway
nginx·spring·gateway
晏宁科技YaningAI1 天前
分布式通信系统的容错机制
网络协议·微服务·系统架构·gateway·信息与通信·paas
爱淋雨的男人3 天前
网关gateway详解
gateway
不吃鱼的猫7483 天前
Janus WebRTC Gateway -- 从零搭建完整指南
gateway·webrtc
zhojiew3 天前
关于AWS Direct Connect with Transit Gateway和Direct Connect Gateway
云计算·gateway·aws
亚马逊云开发者3 天前
GameLift Servers DDoS防护实战:Player Gateway + Ping Beacons延迟优化 + C++ SDK集成
c++·gateway·ddos
戮戮4 天前
Spring Cloud Gateway 零拷贝参数校验:一种高性能网关架构实践
java·网络·架构·gateway
记忆张量MemTensor4 天前
SkillsVote:从技能目录走向终极技能网关(Ultimate Skill Gateway)
gateway
w6100104665 天前
CKA-2026-Gateway
kubernetes·gateway·cka