参考:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-cloud-gateway.html#_gatewayfilter_factories

AddRequestHeader GatewayFilter Factory

为原始请求添加Header,配置示例:为原始请求添加名为 X-Request-Foo ,值为 Bar 的请求头

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: add_request_header_route
  6. uri: https://example.org
  7. filters:
  8. - AddRequestHeader=X-Request-Foo, Bar

AddRequestParameter GatewayFilter Factory

为原始请求添加请求参数及值,配置示例:为原始请求添加名为foo,值为bar的参数,即:foo=bar

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: add_request_parameter_route
  6. uri: https://example.org
  7. filters:
  8. - AddRequestParameter=foo, bar

AddResponseHeader GatewayFilter Factory

为原始响应添加Header,配置示例:为原始响应添加名为 X-Request-Foo ,值为 Bar 的响应头

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: add_response_header_route
  6. uri: https://example.org
  7. filters:
  8. - AddResponseHeader=X-Response-Foo, Bar

DedupeResponseHeader GatewayFilter Factory

我们在Gateway以及微服务上都设置了CORS(解决跨域)Header的话,如果不做任何配置,那么请求 -> 网关 -> 微服务,获得的CORS Header的值,就将会是这样的:

  1. Access-Control-Allow-Credentials: true, true
  2. Access-Control-Allow-Origin: https://musk.mars, https://musk.mars

可以看到这两个Header的值都重复了,若想把这两个Header的值去重的话,就需要使用到DedupeResponseHeader,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: dedupe_response_header_route
  6. uri: https://example.org
  7. filters:
  8. # 若需要去重的Header有多个,使用空格分隔
  9. - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin

去重策略:

  • RETAIN_FIRST:默认值,保留第一个值
  • RETAIN_LAST:保留最后一个值
  • RETAIN_UNIQUE:保留所有唯一值,以它们第一次出现的顺序保留

PS:Spring Cloud Greenwich SR2提供的新特性,低于这个版本无法使用。

Hystrix GatewayFilter Factory

为路由引入Hystrix的断路器保护,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: hystrix_route
  6. uri: https://example.org
  7. filters:
  8. - Hystrix=myCommandName

PS:Hystrix是Spring Cloud第一代容错组件,不过已经进入维护模式,未来Hystrix会被Spring Cloud移除掉,取而代之的是Alibaba Sentinel/Resilience4J。

FallbackHeaders GatewayFilter Factory

同样是对Hystrix的支持,上一小节所介绍的过滤器工厂支持一个配置参数:fallbackUri,该配置用于当发生异常时将请求转发到一个特定的uri上。而FallbackHeaders这个过滤工厂可以在转发请求到该uri时添加一个Header,这个Header的值为具体的异常信息。配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: ingredients
  6. uri: lb://ingredients
  7. predicates:
  8. - Path=//ingredients/**
  9. filters:
  10. - name: Hystrix
  11. args:
  12. name: fetchIngredients
  13. fallbackUri: forward:/fallback
  14. - id: ingredients-fallback
  15. uri: http://localhost:9994
  16. predicates:
  17. - Path=/fallback
  18. filters:
  19. - name: FallbackHeaders
  20. args:
  21. executionExceptionTypeHeaderName: Test-Header

PrefixPath GatewayFilter Factory

为原始的请求路径添加一个前缀路径,配置示例:该配置使访问${GATEWAY_URL}/hello 会转发到https://example.org/mypath/hello

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: prefixpath_route
  6. uri: https://example.org
  7. filters:
  8. - PrefixPath=/mypath

PreserveHostHeader GatewayFilter Factory

为请求添加一个preserveHostHeader=true的属性,路由过滤器会检查该属性以决定是否要发送原始的Host Header。配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: preserve_host_route
  6. uri: https://example.org
  7. filters:
  8. - PreserveHostHeader

如果不设置,那么名为 Host 的Header将由Http Client控制

RequestRateLimiter GatewayFilter Factory

用于对请求进行限流,限流算法为令牌桶。配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: requestratelimiter_route
  6. uri: https://example.org
  7. filters:
  8. - name: RequestRateLimiter
  9. args:
  10. redis-rate-limiter.replenishRate: 10
  11. redis-rate-limiter.burstCapacity: 20

RedirectTo GatewayFilter Factory

将原始请求重定向到指定的Url,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: redirect_route
  6. uri: https://example.org
  7. filters:
  8. - RedirectTo=302, https://acme.org

该配置使访问 ${GATEWAY_URL}/hello 会被重定向到 https://acme.org/hello ,并且携带一个 Location:http://acme.org 的Header,而返回客户端的HTTP状态码为302

PS:

  • HTTP状态码应为3xx,例如301
  • URL必须是合法的URL,该URL会作为Location Header的值

RemoveHopByHopHeadersFilter GatewayFilter Factory

为原始请求删除IETF组织规定的一系列Header,默认删除的Header如下:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailer
  • Transfer-Encoding
  • Upgrade

    可以通过配置去指定仅删除哪些Header,配置示例:
  1. spring:
  2. cloud:
  3. gateway:
  4. filter:
  5. remove-hop-by-hop:
  6. # 多个Header使用逗号(,)分隔
  7. headers: Connection,Keep-Alive

RemoveRequestHeader GatewayFilter Factory

为原始请求删除某个Header,配置示例:删除原始请求中名为 X-Request-Foo 的请求头

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: removerequestheader_route
  6. uri: https://example.org
  7. filters:
  8. - RemoveRequestHeader=X-Request-Foo

RemoveResponseHeader GatewayFilter Factory

为原始响应删除某个Header,配置示例:删除原始响应中名为 X-Request-Foo 的响应头

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: removeresponseheader_route
  6. uri: https://example.org
  7. filters:
  8. - RemoveResponseHeader=X-Response-Foo

RewritePath GatewayFilter Factory

通过正则表达式重写原始的请求路径,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: rewritepath_route
  6. uri: https://example.org
  7. predicates:
  8. - Path=/foo/**
  9. filters:
  10. # 参数1为原始路径的正则表达式,参数2为重写后路径的正则表达式
  11. - RewritePath=/foo/(?<segment>.*), /$\{segment}

该配置使得访问 /foo/bar 时,会将路径重写为/bar 再进行转发,也就是会转发到 https://example.org/bar。需要注意的是:由于YAML语法,需用$\ 替换 $

RewriteResponseHeader GatewayFilter Factory

重写原始响应中的某个Header,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: rewriteresponseheader_route
  6. uri: https://example.org
  7. filters:
  8. # 参数1为Header名称,参数2为值的正则表达式,参数3为重写后的值
  9. - RewriteResponseHeader=X-Response-Foo, password=[^&]+, password=***

该配置的意义在于:如果响应头中 X-Response-Foo 的值为/42?user=ford&password=omg!what&flag=true,那么就会被按照配置的值重写成/42?user=ford&password=&flag=true,也就是把其中的password=omg!what重写成了password=

SaveSession GatewayFilter Factory

在转发请求之前,强制执行WebSession::save操作,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: save_session
  6. uri: https://example.org
  7. predicates:
  8. - Path=/foo/**
  9. filters:
  10. - SaveSession

主要用在那种像 Spring Session 延迟数据存储(数据不是立刻持久化)的,并希望在请求转发前确保session状态保存情况。如果你将Spring Secutiry于Spring Session集成使用,并想确保安全信息都传到下游机器,就需要配置这个filter。

secureHeaders GatewayFilter Factory

secureHeaders过滤器工厂为原始响应添加了一系列起安全作用的响应头。默认会添加如下Headers(包括值):

  • X-Xss-Protection:1; mode=block
  • Strict-Transport-Security:max-age=631138519
  • X-Frame-Options:DENY
  • X-Content-Type-Options:nosniff
  • Referrer-Policy:no-referrer
  • Content-Security-Policy:default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'
  • X-Download-Options:noopen
  • X-Permitted-Cross-Domain-Policies:none

如果你想修改这些Header的值,那么就需要使用这些Headers对应的后缀,如下:

  • xss-protection-header
  • strict-transport-security
  • frame-options
  • content-type-options
  • referrer-policy
  • content-security-policy
  • download-options
  • permitted-cross-domain-policies

配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. filter:
  5. secure-headers:
  6. # 修改 X-Xss-Protection 的值为 2; mode=unblock
  7. xss-protection-header: 2; mode=unblock

如果想禁用某些Header,可使用如下配置:

  1. spring:
  2. cloud:
  3. gateway:
  4. filter:
  5. secure-headers:
  6. # 多个使用逗号(,)分隔
  7. disable: frame-options,download-options

SetPath GatewayFilter Factory

修改原始的请求路径,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: setpath_route
  6. uri: https://example.org
  7. predicates:
  8. - Path=/foo/{segment}
  9. filters:
  10. - SetPath=/{segment}

该配置使访问 ${GATEWAY_URL}/foo/bar 时会转发到 https://example.org/bar ,也就是原本的/foo/bar被修改为了/bar

SetResponseHeader GatewayFilter Factory

修改原始响应中某个Header的值,配置示例:将原始响应中 X-Response-Foo 的值修改为 Bar

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: setresponseheader_route
  6. uri: https://example.org
  7. filters:
  8. - SetResponseHeader=X-Response-Foo, Bar

SetStatus GatewayFilter Factory

修改原始响应的状态码,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: setstatusstring_route
  6. uri: https://example.org
  7. filters:
  8. # 字符串形式
  9. - SetStatus=BAD_REQUEST
  10. - id: setstatusint_route
  11. uri: https://example.org
  12. filters:
  13. # 数字形式
  14. - SetStatus=401

SetStatusd的值可以是数字,也可以是字符串。但一定要是Spring HttpStatus 枚举类中的值。上面这两种配置都可以返回401这个HTTP状态码。

StripPrefix GatewayFilter Factory

用于截断原始请求的路径,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: nameRoot
  6. uri: http://nameservice
  7. predicates:
  8. - Path=/name/**
  9. filters:
  10. # 数字表示要截断的路径的数量
  11. - StripPrefix=2

如果请求的路径为 /name/bar/foo ,那么则会截断成/foo后进行转发 ,也就是会截断2个路径。

Retry GatewayFilter Factory

针对不同的响应进行重试,例如可以针对HTTP状态码进行重试,配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: retry_test
  6. uri: http://localhost:8080/flakey
  7. predicates:
  8. - Host=*.retry.com
  9. filters:
  10. - name: Retry
  11. args:
  12. retries: 3
  13. statuses: BAD_GATEWAY

可配置如下参数:

  • retries:重试次数
  • statuses:需要重试的状态码,取值在 org.springframework.http.HttpStatus 中
  • methods:需要重试的请求方法,取值在 org.springframework.http.HttpMethod 中
  • series:HTTP状态码序列,取值在 org.springframework.http.HttpStatus.Series 中

RequestSize GatewayFilter Factory

设置允许接收最大请求包的大小,配置示例:如果请求包大小超过设置的值,则会返回 413 Payload Too Large以及一个errorMessage

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: request_size_route
  6. uri: http://localhost:8080/upload
  7. predicates:
  8. - Path=/upload
  9. filters:
  10. - name: RequestSize
  11. args:
  12. # 单位为字节
  13. maxSize: 5000000

Modify Request Body GatewayFilter Factory

在转发请求之前修改原始请求体内容,该过滤器工厂只能通过代码配置,不支持在配置文件中配置。代码示例:

  1. @Bean
  2. public RouteLocator routes(RouteLocatorBuilder builder) {
  3. return builder.routes()
  4. .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
  5. .filters(f -> f.prefixPath("/httpbin")
  6. .modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
  7. (exchange, s) -> return Mono.just(new Hello(s.toUpperCase())))).uri(uri))
  8. .build();
  9. }
  10. static class Hello {
  11. String message;
  12. public Hello() { }
  13. public Hello(String message) {
  14. this.message = message;
  15. }
  16. public String getMessage() {
  17. return message;
  18. }
  19. public void setMessage(String message) {
  20. this.message = message;
  21. }
  22. }

该过滤器工厂处于 BETA 状态,未来API可能会变化,生产环境慎用

Modify Response Body GatewayFilter Factory

可用于修改原始响应体的内容,该过滤器工厂同样只能通过代码配置,不支持在配置文件中配置。代码示例:

  1. @Bean
  2. public RouteLocator routes(RouteLocatorBuilder builder) {
  3. return builder.routes()
  4. .route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
  5. .filters(f -> f.prefixPath("/httpbin")
  6. .modifyResponseBody(String.class, String.class,
  7. (exchange, s) -> Mono.just(s.toUpperCase()))).uri(uri)
  8. .build();
  9. }

该过滤器工厂处于 BETA 状态,未来API可能会变化,生产环境慎用

Default Filters

Default Filters用于为所有路由添加过滤器工厂,也就是说通过Default Filter所配置的过滤器工厂会作用到所有的路由上。配置示例:

  1. spring:
  2. cloud:
  3. gateway:
  4. default-filters:
  5. - AddResponseHeader=X-Response-Default-Foo, Default-Bar
  6. - PrefixPath=/httpbin

Spring Cloud Alibaba学习笔记(18) - Spring Cloud Gateway 内置的过滤器工厂的更多相关文章

  1. Spring Cloud Alibaba学习笔记(19) - Spring Cloud Gateway 自定义过滤器工厂

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的内置过滤器工厂,若Spring Cloud Gateway内置的过滤器工厂无法满足我们的业务需求,那么此时就需要自定义自己的过 ...

  2. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  3. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  4. Flutter学习笔记(36)--常用内置动画

    如需转载,请注明出处:Flutter学习笔记(36)--常用内置动画 Flutter给我们提供了很多而且很好用的内置动画,这些动画仅仅需要简单的几行代码就可以实现一些不错的效果,Flutter的动画分 ...

  5. Spring Cloud Alibaba学习笔记(1) - 整合Spring Cloud Alibaba

    Spring Cloud Alibaba从孵化器版本毕业:https://github.com/alibaba/spring-cloud-alibaba,记录一下自己学习Spring Cloud Al ...

  6. Spring Cloud Alibaba学习笔记(21) - Spring Cloud Gateway 自定义全局过滤器

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的全局过滤器,本文介绍如何自定义全局过滤器. 自定义全局过滤需要实现GlobalFilter 接口,该接口和 GatewayFi ...

  7. Spring Cloud Alibaba学习笔记(17) - Spring Cloud Gateway 自定义路由谓词工厂

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的路由谓词工厂,但是如果这些内置的路由谓词工厂不能满足业务需求的话,我们可以自定义路由谓词工厂来实现特定的需求. 例如有某个服务 ...

  8. Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂

    Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...

  9. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

随机推荐

  1. html5获取用户当前位置

    支持地理定位的浏览器有IE9+.Firefox 3.5+ .Opera 10.6+ .Safari 5+ .Chrome.iOS 版Safari.Android版WebKit. navigator.g ...

  2. 错误: 找不到或无法加载主类 Welcome.java

    问题原因: 不需要带.java

  3. Healthcare in Azure

  4. pyqt5设置背景图片出现问题

    在使用pyqt5时,用qtdesign设置好背景图片,如何设置自行百度,预览没问题,用ptuic5转换为代码却发现显示不了: 首先:我在qtdesign中导入的是pic.qrc,但是转换的代码最后一句 ...

  5. Invalid bound statement (not found) 错误原因

    对我来说,错误的原因是因为没有配置:mybatis.mapperLocations=classpath:mybatis/mapper/*Mapper.xmlmybatis.config-locatio ...

  6. cnetos7 搭建wordpress(apache+php+mariadb)

    .安装apache.php.php库 yum -y install httpd php php-mbstring php-pear 2.修改php配置文件地区 vim /etc/php.ini 在87 ...

  7. Cucumber介绍

    Cucumber是一个提供能让我们都理解的普通语言,通过普通语言来描述的测试用例,并支持行为驱动开发的测试工具.Cucumber支持大多数变成语言,如Ruby.Java和Python等. 官方地址:h ...

  8. SD-WAN基础---SD-WAN简单了解

    一:推文(摘录.转载自) 关于SD-WAN,你不得不了解的10个常识 那些让人怦然心动的SD-WAN功能(上) 那些让人怦然心动的SD-WAN功能(中) 二:SD-WAN是什么 SD-WAN,即软件定 ...

  9. PAT 甲级 1075 PAT Judge (25分)(较简单,注意细节)

    1075 PAT Judge (25分)   The ranklist of PAT is generated from the status list, which shows the scores ...

  10. precommit那些事儿

    一.使用背景 我们有将 lint 命令添加进 npm scripts 中,但是很多人在提交代码时都会忘记或者没有习惯去执行检查,结果就是导致不符合规范的代码被上传到远端代码仓库. 二.问题分析 我们可 ...