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

相关推荐
不会写DN1 天前
让 gRPC 服务同时支持 HTTP/JSON 的gRPC-Gateway
http·json·gateway
棒棒的唐3 天前
WSL2用npm安装的openclaw,无法正常使用openclaw gateway start启动服务的问题
前端·npm·gateway
wuyikeer4 天前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
2501_948114245 天前
AI API Gateway 选型指南:2026 年生产环境下的聚合平台深度对比
人工智能·gateway
心勤则明5 天前
Spring AI Alibaba MCP Gateway:将存量服务转换成 MCP Server
人工智能·spring·gateway
hutengyi5 天前
当遇到 502 错误(Bad Gateway)怎么办
gateway
无名-CODING6 天前
Java 爬虫零基础入门:从 HTTP 到 Jsoup 实战
spring cloud·gateway·sentinel
standovon7 天前
Gateway Timeout504 网关超时的完美解决方法
gateway
无名-CODING7 天前
SpringCloud 网关与熔断:Gateway + Sentinel 快速入门
spring cloud·gateway·sentinel
会飞的大可7 天前
022 API网关设计:Gateway路由、限流与鉴权实战
gateway