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#中的?问号
    佛学中的108条做人道理
    SQL Server函数总结
    ASP.NET MVC与Web Forms
    常数定义和字段定义的区别
    What is the purpose of Entity Framework T4 template.
    ASP.NET Internals – IIS and the Process Model
    asp.net为什么没有多重继承(个人观点,欢迎指正,谢谢!)
    c#委托,事件及观察者模式(转自:http://www.cnblogs.com/JimmyZhang/archive/2011/12/25/903360.html)
    GC and Memory leak
  • 原文地址:https://www.cnblogs.com/chenglc/p/13131904.html
Copyright © 2011-2022 走看看