zoukankan      html  css  js  c++  java
  • Spring Cloud Gateway 2 断言

    Spring Cloud Gateway 断言

    Spring Cloud Gateway提供了很多断言设置,当http请求进入Spring Cloud Gateway时,网关中的路由断言工厂会根基配置的路由规则,对http请求进行断言匹配,匹配成功的请求进行路由转发,失败的直接返回是错误信息。

    以下是常用的断言工厂

    After

    设定一个UTC时间,此时间之后的请求会成功,此时间之前的请求会404

    UTC 时间生成:

    //比当前时间早一小时的时间
    String minTime = ZonedDateTime.now().minusHours(1).format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
    System.out.println(minTime);
    

    java配置

    @Bean
    public RouteLocator customRoteLocator(RouteLocatorBuilder builder) {
        //生成一个比当前时间早一个小时的UTC时间
        ZonedDateTime minusTime = LocalDateTime.now().minusHours(1).atZone(ZoneId.systemDefault());
        return builder.routes()
                .route("after_route", predicateSpec -> predicateSpec.after(minusTime)
                        .uri("http://baidu.com"))
                .build();
    }
    

    yml配置

    spring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - After=2017-01-20T17:42:47.789-07:00[America/Denver]
    

    Before

    设定一个时间,此时间之前的请求会成功,此时间之后的请求会404

    spring:
      cloud:
        gateway:
          routes:
          - id: before_route
            uri: https://example.org
            predicates:
            - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
    

    Between

    设定一个时间区间,此时间区间内的请求会成功

    spring:
      cloud:
        gateway:
          routes:
          - id: between_route
            uri: https://example.org
            predicates:
            - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
    

    设置两个参数:cookie的key和value,当请求中携带一条此信息时,请求成功;

    spring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - Cookie=mycookie,mycookievalue
    

    配置多个值:

    pring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - name: Cookie
              args:
                name: mycookie
                regexp: mycookievalue
    

    设置两个参数:header的key和value,当请求携带匹配信息时,请求成功

    spring:
      cloud:
        gateway:
          routes:
          - id: header_route
            uri: https://example.org
            predicates:
             - Header=key,value
    

    Host

    设定一个主机名,当请求信息来自此主机时,请求成功;

    spring:
      cloud:
        gateway:
          routes:
          - id: host_route
            uri: https://example.org
            predicates:
            - Host=**.somehost.org,**.anotherhost.org
    

    Method

    指定请求的方法:如POST,GET

    spring:
      cloud:
        gateway:
          routes:
          - id: method_route
            uri: https://example.org
            predicates:
            - Method=GET,POST
    

    Path

    指定路径下的请求,进行转发

    spring:
      cloud:
        gateway:
          routes:
          - id: path_route
            uri: https://example.org
            predicates:
            - Path=/red/{segment},/blue/{segment}
    

    Query

    指定一个请求参数的key,符合条件的进行转发

    如:http://localhost:8080/hello?key=value 中的key

    spring:
      cloud:
        gateway:
          routes:
          - id: query_route
            uri: https://example.org
            predicates:
            - Query=key
    
    spring:
      cloud:
        gateway:
          routes:
          - id: query_route
            uri: https://example.org
            predicates:
            - Query=red, gree.
    

    RemoteAddr

    指定ip或ip段,符合条件的转发成功

    spring:
      cloud:
        gateway:
          routes:
          - id: remoteaddr_route
            uri: https://example.org
            predicates:
            - RemoteAddr=192.168.1.1/24
    
  • 相关阅读:
    数据结构(二):线性表的使用原则以及链表的应用-稀疏矩阵的三元组表示
    数据结构(二):线性表的使用原则以及链表的应用-稀疏矩阵的三元组表示
    图像相似度测量与模板匹配总结
    图像相似度测量与模板匹配总结
    C/C++文件操作经验总结
    C/C++文件操作经验总结
    C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
    C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
    数据结构(二):链表、链队列
    数据结构(二):链表、链队列
  • 原文地址:https://www.cnblogs.com/chenglc/p/13131904.html
Copyright © 2011-2022 走看看