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

相关推荐
JJJennie7772 天前
从苹果 2026 落地场景,看系统级 Agent 时代的隐私边界与 MAI Gateway 的企业Token治理
人工智能·gateway·apple
kakawzw2 天前
微服务组件源码6——Spring Gateway
spring·gateway
白露与泡影2 天前
Java 8老系统旁路接入AI Gateway:不升级JDK也能用AI
java·人工智能·gateway
是一个Bug3 天前
Nginx 与 API Gateway:从“小区门卫”到“商场总服务台”
运维·nginx·gateway
大G的笔记本3 天前
生产级 Spring Boot 网关完整实现方案
java·笔记·gateway
暗夜猎手-大魔王5 天前
转载--Hermes Agent 13 | Gateway 架构:二十余渠道如何复用同一套 Agent Runtime
人工智能·gateway
YJlio6 天前
OpenClaw 2026.5.2 Beta 更新解读:外部插件安装、ClawHub / npm 切换与 Gateway 性能优化
性能优化·npm·gateway·飞书·多维表格·飞书aily·飞书妙搭
v***59836 天前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
团子的二进制世界6 天前
Gateway :微服务架构的核心网关
微服务·架构·gateway
MrMonkeyHou6 天前
Java微服务架构中的双剑合璧:Nacos与Gateway深度解析
java·微服务·架构·gateway