整合Spring Cloud Gateway实现断言过滤跨域

本文详细介绍了SpringCloudGateway作为API网关的作用,包括路由、断言和过滤器的概念。通过实例展示了如何在项目中集成SpringCloudGateway,包括配置注册中心、跨域设置和路由规则。同时,提供了配置跨域和路由规则的代码示例。
摘要由CSDN通过智能技术生成

1.什么是Spring Cloud Gateway

本项目提供了一个构建在 Spring 生态之上的 API Gateway,包括:Spring 5、Spring Boot 2 和 Project Reactor。Spring Cloud Gateway 旨在提供一种简单而有效的方式来路由到 API,并为它们提供横切关注点,例如:安全性、监控/指标和可用性。

官方文档Spring Cloud GatewaySpring Cloud Gatewayhttps://docs.spring.io/spring-cloud-gateway/docs/3.0.4/reference/html/

2.词汇表

路由:网关的基本构建块。它由 ID、目标 URI、断言集合和过滤器集合定义。如果聚合匹配为真,则匹配路由。

断言:这是一个Java 8 函数断言。输入类型是Spring FrameworkServerWebExchange。这使您可以匹配来自 HTTP 请求的任何内容,例如标头或参数。

过滤:这些是使用特定工厂构建的实例。在这里,您可以在发送下游请求之前或之后修改请求和响应。

3.工作流程

客户端向 Spring Cloud Gateway 发出请求。如果网关处理程序映射确定请求与路由匹配,则将其发送到网关 Web 处理程序。此处理程序通过特定于请求的过滤器链运行请求。过滤器用虚线划分的原因是过滤器可以在发送代理请求之前和之后运行逻辑。执行所有“预”过滤器逻辑。然后发出代理请求。发出代理请求后,将运行“发布”过滤器逻辑。

tips:在没有端口的路由中定义的 URI 分别获得 HTTP 和 HTTPS URI 的默认端口值 80 和 443。

4.整合进项目

4.1 新建项目

@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GulimallGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallGatewayApplication.class, args);
    }

}

4.2 导入Spring Cloud Gateway依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>3.0.4</version>
        </dependency>

 4.3 新建application.properties

配置注册中心

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-gateway
server.port=88

4.4 配置跨域设置

跨域配置详情可见官网

Spring Cloud Gatewayhttps://docs.spring.io/spring-cloud-gateway/docs/3.0.4/reference/html/#cors-configuration

/**
 * @author wangli
 * @create 2021-12-05 16:46
 */
@Configuration
public class GulimallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }

}

4.5 配置路由规则

断言过滤规则详情可见下发

Spring Cloud Gatewayhttps://docs.spring.io/spring-cloud-gateway/docs/3.0.4/reference/html/#gateway-request-predicates-factories

spring:
  cloud:
    gateway:
      routes:
        - id: ware_route
          uri: lb://gulimall-ware
          predicates:
            - Path=/api/ware/**
          filters:
            - RewritePath=/api(?<segment>/?.*),/$\{segment}

        - id: member_route
          uri: lb://gulimall-member
          predicates:
            - Path=/api/member/**
          filters:
            - RewritePath=/api(?<segment>/?.*),/$\{segment}

        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api(?<segment>/?.*),/$\{segment}

        - id: third_party_route
          uri: lb://gulimall-third-party
          predicates:
            - Path=/api/thirdparty/**
          filters:
            - RewritePath=/api/thirdparty(?<segment>/?.*),/$\{segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api(?<segment>/?.*),/renren-fast/$\{segment}

        - id: gulimall_host_route
          uri: lb://gulimall-product
          predicates:
            - Host=gulimall.com,item.gulimall.com

        - id: gulimall_search_route
          uri: lb://gulimall-search
          predicates:
            - Host=search.gulimall.com

        - id: gulimall_auth_route
          uri: lb://gulimall-auth-server
          predicates:
            - Host=auth.gulimall.com

        - id: gulimall_cart_router
          uri: lb://gulimall-cart
          predicates:
            - Host=cart.gulimall.com

        - id: gulimall_order_router
          uri: lb://gulimall-order
          predicates:
            - Host=order.gulimall.com

路由规则可以根据需求自己配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨会停rain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值