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

相关推荐
yuluo_YX12 天前
AI Gateway 介绍
人工智能·gateway
网硕互联的小客服15 天前
502 Bad Gateway:服务器作为网关或代理时收到无效响应处理方式
运维·服务器·gateway
sevevty-seven15 天前
什么是Gateway
gateway
秋の花16 天前
【GateWay】和权限验证
java·gateway
欧先生^_^17 天前
org.springframework.cloud.gateway 组件解释
gateway
jarenyVO18 天前
Spring Cloud Gateway 全面学习指南
java·gateway
保持学习ing21 天前
微服务--Gateway网关
java·网关·微服务·gateway
SZ17011023121 天前
IGP(Interior Gateway Protocol,内部网关协议)
运维·服务器·gateway
肥仔哥哥193021 天前
SpringCloud2025+SpringBoot3.5.0+gateway+webflux子服务路由报503
微服务·gateway·最新微服务
亚林瓜子1 个月前
AWS API Gateway配置日志
云计算·gateway·aws·log·cloudwatch