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

相关推荐
半支烟隐 pzishuo1 天前
Kubernetes Gateway API 完全指南
容器·kubernetes·gateway
lee_curry1 天前
RuoYi Cloud:Gateway、Auth、System 接入 Prometheus JVM 监控,并练习多副本、滚动升级和故障恢复
jvm·gateway·prometheus
mqiqe1 天前
构建企业级 AI 原生架构:LLM Gateway、RAG、Agent 与 MCP 的协同关系解析
人工智能·架构·gateway
mqiqe2 天前
LLM Gateway:企业级大模型应用的“统一控制平面”
平面·flask·gateway
敲代码的嘎仔2 天前
从零搭建微服务:Spring Cloud Alibaba 全家桶踩坑实录(Nacos + OpenFeign + Gateway + Sentinel)
java·spring boot·spring·spring cloud·微服务·gateway·sentinel
JJJennie7777 天前
【MAI Gateway|Finapi】AI网关财务管理,更细致的模型调用管理
人工智能·gateway·ai网关
小罗水9 天前
第4章 JWT 登录认证与 Gateway 统一鉴权
gateway
zhougl99610 天前
Gateway 和 Nginx 路由区别
运维·nginx·gateway
沉迷学习 日益消瘦10 天前
10-Gateway API
运维·kubernetes·gateway
nvd1111 天前
基于 ArgoCD 优雅落地 K8s Gateway API 与 Kong 控制器(KIC)
kubernetes·gateway·argocd