摘要:欢迎访问我的欢迎访问我的内容所有原创文章分类汇总及配套源码,涉及等本篇概览本篇概览作为实战系列的第五篇,是时候了解过滤器的作用了,本篇咱们一起来了解内置好的过滤器,真是种类繁多功能强大过滤器顾名思义,就是在请求头部添加指定的内容带有的完整配
https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - AddRequestHeader=x-request-foo, bar-config
[ { "id": "path_route_addr", "uri": "http://127.0.0.1:8082", "predicates": [ { "name": "Path", "args": { "pattern": "/hello/**" } } ], "filters": [ { "name": "AddRequestHeader", "args": { "name": "x-request-foo", "value": "bar-dynamic" } } ] }]
AddRequestParameter过滤器顾名思义,就是添加请求参数
配置如下,服务提供方收到的请求中会多一个参数,名为foo,值为bar-config:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - AddRequestParameter=foo, bar-config
[ { "id": "path_route_addr", "uri": "http://127.0.0.1:8082", "predicates": [ { "name": "Path", "args": { "pattern": "/hello/**" } } ], "filters": [ { "name": "AddRequestParameter", "args": { "name": "foo", "value": "bar-dynamic" } } ] }]
AddResponseHeader过滤器就是在响应的header中添加参数
配置如下,客户端收到的响应,其header中会多一个参数,名为foo,值为bar-config-response:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - AddResponseHeader=foo, bar-config-response
[ { "id": "path_route_addr", "uri": "http://127.0.0.1:8082", "predicates": [ { "name": "Path", "args": { "pattern": "/hello/**" } } ], "filters": [ { "name": "AddResponseHeader", "args": { "name": "foo", "value": "bar-dynamic-response" } } ] }]
服务提供方返回的response的header中,如果有的key出线了多个value(例如跨域场景下的Access-Control-Allow-Origin),DedupeResponseHeader过滤器可以将重复的value剔除调,剔除策略有三种:RETAIN_FIRST (保留第一个,默认), RETAIN_LAST(保留最后一个), RETAIN_UNIQUE(去重)
配置如下,指定了两个header key的去重,策略是保留最后一个:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin, RETAIN_LAST
服务提供方返回的response的header中,如果有的key出线了多个value(例如跨域场景下的Access-Control-Allow-Origin),DedupeResponseHeader过滤器可以将重复的value剔除调,剔除策略有三种:RETAIN_FIRST (保留第一个,默认), RETAIN_LAST(保留最后一个), RETAIN_UNIQUE(去重)
配置如下,指定了两个header key的去重,策略是保留最后一个:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin, RETAIN_LAST
spring: cloud: gateway: routes: - id: ingredients uri: lb://ingredients predicates: - Path=//ingredients/** filters: - name: CircuitBreaker args: name: fetchIngredients fallbackUri: forward:/fallback - id: ingredients-fallback uri: http://localhost:9994 predicates: - Path=/fallback filters: - name: FallbackHeaders args: executionExceptionTypeHeaderName: Test-Header
MapRequestHeader用于header中的键值对复制,如下配置的意思是:如果请求header中有Blue就新增名为X-Request-Red的key,其值和Blue的值一样
配置如下,指定了两个header key的去重,策略是保留最后一个:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - MapRequestHeader=Blue, X-Request-Red
PrefixPath很好理解,就是转发到服务提供者的时候,给path加前缀
例如我这边服务提供者原始地址是http://127.0.0.1:8082/hello/str配置如下,如果我给网关配置PrefixPath=hello,那么访问网关的时候,请求路径中就不需要hello了,配置如下:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/str filters: - PrefixPath=/hello
PreserveHostHeader在转发请求到服务提供者的时候,会保留host信息(否则就只能由HTTP client来决定了)
先看不使用PreserveHostHeader的效果,如下图,服务提供者收到的请求header中的host就是网关配置的信息:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.1.1.1:11111 predicates: - Path=/hello/** filters: - RedirectTo=302, http://127.0.0.1:8082/hello/str
RemoveRequestHeader很好理解,删除请求header中的指定值
下面的配置会删除请求header中的foo:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - RemoveRequestHeader=foo
RemoveResponseHeader删除响应header中的指定值
下面的配置会删除响应header中的foo:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - RemoveResponseHeader=foo
RemoveRequestParameter 删除请求参数中的指定参数
下面的配置会删除请求参数中的foo:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - RemoveRequestParameter=foo1
RewritePath非常实用,将请求参数中的路径做变换
下面的配置会将/test/str转成/hello/str:
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/test/** filters: - RewritePath=/test/?(?.*), /hello/$/{segment}
RewriteLocationResponseHeader用于改写response中的location信息
配置如下,一共是四个参数:stripVersionMode、locationHeaderName、hostValue、protocolsRegex
例如请求是api.example.com/some/object/name,response的location是object-service.prod.example.net/v2/some/object/id,最终会被下面的filter改写为api.example.com/some/object/id
spring: cloud: gateway: routes: - id: rewritelocationresponseheader_route uri: http://example.org filters: - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
NEVER_STRIP:不执行
AS_IN_REQUEST :原始请求没有vesion,就执行
ALWAYS_STRIP :固定执行
Location用于替换host:port部分,如果没有就是用Request中的host
protocolsRegex用于匹配协议,如果匹配不上,name过滤器啥都不做
RewriteResponseHeader很好理解:修改响应header,参数有三个:header的key,匹配value的正则表达式,修改value的结果
下面的配置表示修改响应header中X-Response-Red这个key的value,找到password=xxx的内容,改成password=***
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/test/** filters: - RewriteResponseHeader=X-Response-Red, , password=[^&]+, password=***
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SecureHeaders
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/test/{segment} filters: - SetPath=/hello/{segment}
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetRequestHeader=X-Request-Red, Blue
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/{segment} filters: - SetRequestHeader=X-Request-Red, Blue-{segment}
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: filter: secure-headers: disable: - x-frame-options - strict-transport-security routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetResponseHeader=X-Request-Red, Blue
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetStatus=500
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: set-status: original-status-header-name: aaabbbccc routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - SetStatus=500
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: set-status: original-status-header-name: aaabbbccc routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/aaa/** filters: - StripPrefix=2
spring: cloud: gateway: routes: - id: retry_test uri: http://localhost:8080/flakey predicates: - Host=*.retry.com filters: - name: Retry args: retries: 3 statuses: BAD_GATEWAY methods: GET,POST backoff: firstBackoff: 10ms maxBackoff: 50ms factor: 2 basedOnPreviousValue: false
spring: cloud: gateway: routes: - id: request_size_route uri: http://localhost:8080/upload predicates: - Path=/upload filters: - name: RequestSize args: maxSize: 5000000
SetRequestHostHeader会修改请求header中的host值
下面的配置,会将请求header中的host改为aaabbb
server: #服务端口 port: 8081spring: application: name: hello-gateway cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/** filters: - name: SetRequestHostHeader args: host: aaabbb
@Beanpublic RouteLocator routes(RouteLocatorBuilder builder) { return builder.routes() .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org") .filters(f -> f.prefixPath("/httpbin") .modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE, (exchange, s) -> return Mono.just(new Hello(s.toUpperCase())))).uri(uri)) .build();}static class Hello { String message; public Hello() { } public Hello(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }}
@Beanpublic RouteLocator routes(RouteLocatorBuilder builder) { return builder.routes() .route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org") .filters(f -> f.prefixPath("/httpbin") .modifyResponseBody(String.class, String.class, (exchange, s) -> Mono.just(s.toUpperCase()))).uri(uri)) .build();}
spring: cloud: gateway: routes: - id: resource uri: http://localhost:9000 predicates: - Path=/resource filters: - TokenRelay=
spring: cloud: gateway: default-filters: - AddResponseHeader=X-Response-Default-Red, Default-Blue - PrefixPath=/httpbin
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/123758.html
摘要:欢迎访问我的欢迎访问我的内容所有原创文章分类汇总及配套源码,涉及等本篇概览本篇概览作为实战系列的第九篇,咱们聊聊如何用修改原始请求和响应内容,以及修改过程中遇到的问题首先是修改请求,如下图,浏览器是请求发起方,真实参数只有,经过网关时被塞欢迎访问我的GitHubhttps://github.com/zq2599/blog_demos内容:所有原创文章分类汇总及配套源码,涉及Java、Dock...
摘要:类似的工厂类还有和,,,这几个就不做单独讲解了,使用方式是一样的。配置示列配置示列讨论时间文章中讲的这几个工厂类的作用我们已经了解了,那具体的使用场景有哪些适合在什么场景下使用呢欢迎大家留言讨论。 今天我们来学习下GatewayFilter Factory,中文解释就是过滤器工厂。 官方文档对GatewayFilter Factory的介绍: Route filters allow t...
摘要:欢迎访问我的欢迎访问我的内容所有原创文章分类汇总及配套源码,涉及等本篇概览本篇概览本文是实战系列的第八篇,经过前面的学习,咱们对过滤器已了解得差不多,今天来补全过滤器的最后一个版块限流默认的限流器是基于实现的,限流算法是大家熟悉的令牌桶关于欢迎访问我的GitHubhttps://github.com/zq2599/blog_demos内容:所有原创文章分类汇总及配套源码,涉及Java、Doc...
阅读 1729·2023-04-25 23:43
阅读 907·2021-11-24 09:39
阅读 712·2021-11-22 15:25
阅读 1708·2021-11-22 12:08
阅读 1083·2021-11-18 10:07
阅读 2065·2021-09-23 11:22
阅读 3337·2021-09-22 15:23
阅读 2468·2021-09-13 10:32