当前位置:网站首页>【微服务|SCG】Filters的33种用法
【微服务|SCG】Filters的33种用法
2022-07-05 02:29:00 【步尔斯特】
文章目录
增加请求头/请求参数/响应头
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: ossa-service-producer
uri: lb://ossa-service-producer
predicates:
- Path=/gw/producer/**
- After=2022-03-03T15:50:30.337+08:00[Asia/Shanghai]
- Before=2024-03-03T15:50:30.337+08:00[Asia/Shanghai]
filters:
- StripPrefix=1
# 增加请求头、请求参数
- AddRequestHeader=origin-header,producer-header
- AddRequestParameter= origin-param,producer-param
- AddResponseHeader=rep-header,rep-header
新增动态header请求头
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: ossa-service-producer
uri: lb://ossa-service-producer
predicates:
- Path=/gw/producer/{
id}
- After=2022-03-03T15:50:30.337+08:00[Asia/Shanghai]
- Before=2024-03-03T15:50:30.337+08:00[Asia/Shanghai]
filters:
- StripPrefix=1
# 增加请求头、请求参数
- AddRequestHeader=origin-header,producer-header-{
id}
设置请求头、过滤器配置,修改移除请求头
filters:
- StripPrefix=1
# 增加请求头、请求参数
- AddRequestHeader=origin-header,producer-header-{
id}
- AddRequestParameter=origin-param,producer-param
- AddResponseHeader=rep-header,rep-header
# 设置请求头,没有则新增,有则修改
- SetRequestHeader=X-Request-id,value
# 移除请求头、请求参数
- RemoveRequestHeader=X-Request-id
- RemoveRequestParameter=X-Request
- RemoveRequestParameter=X-Request-author
- AddResponseHeader=req-param,req-param
# 先从header取from-Header,如果有则赋值给to-Header,如果没from-Header则无效果
- MapRequestHeader=from-Header,to-Header
PreserveHostHeader
默认开启,在gateway转发请求前把原始请求的host头部带上,转发给目的服务。
spring:
cloud:
gateway:
routes:
- id: preserve_host_route
uri: https://example.org
filters:
- PreserveHostHeader
重写response加密密码
#例如: /42?user=ford&passwd=omg!what&flag=true 经过下面配置替换为/42?user=ford&passwd=***&flag=true
spring:
cloud:
gateway:
routes:
- id: rewriteresponseheader_route
uri: https://example.org
filters:
- RewriteResponseHeader=X-Response-Red, , password=[^&]+, password=***
response去重
RETAIN_FIRST保留第一个(default)
RETAIN_LAST保留最后一个
RETAIN_FIRST RETAIN_UNIQUE 保留唯一
spring:
cloud:
gateway:
routes:
- id: dedupe_response_header_route
uri: https://example.org
filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
路径带前缀
请求/ 其实是/mypath
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=/mypath
配置30几 跳转到指定地址
#300 Multiple多种选择,请求的资源可能包括多个位置,相应可返回一个资源特征与地址的列表用于用户终端选择
#301 Moved Permanently永久移动,请求的资源已被永久移动新位置,返回信息会包含URI,浏览器会自动定向到新地址,以后请求应该用新的URI代替
#302 Found 临时移动,与301类似,资源临时被移动,客户端应该继续使用原URI
#303 See Other查看其它地址,与301类似。使用GET和POST请求查看
#304 未修改,所请求资源未修改,服务器返回此状态吗时,不会返回任何资源
#305 Use proxy 使用代理,所请求资源必须通过代理
#306 Unused 已经废弃的HTTP状态码
#307 Temporary Redirect 临时重定向,与302类似,使用GET请求重定向
- RedirectTo=302,http://www.baidu.com
- RedirectTo=301,http://www.taobao.com
修改状态码
修改状态码,可以org.springframework.http.HttpStatus
枚举,也可以直接写状态码
无论哪种情况,响应的 HTTP 状态都设置为 401。
spring:
cloud:
gateway:
routes:
- id: setstatusstring_route
uri: https://example.org
filters:
- SetStatus=UNAUTHORIZED
- id: setstatusint_route
uri: https://example.org
filters:
- SetStatus=401
转发地址
访问/api/getheader 实际访问:/getheader,转发的目的地址必须写 / 否则会出错
- RewritePath=/consumingServiceEndpoint, /backingServiceEndpoint
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: https://example.org
predicates:
- Path=/red/**
filters:
- RewritePath=/red/?(?<segment>.*), /$\{
segment}
去掉\增加请求路径中部分层级
请求/name/first/123 实际请求/123
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/first/**
filters:
# 从前往后去掉一些路径
- StripPrefix=2
请求/name/first/123 实际请求/prefix/name/first/123
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/first/**
filters:
# 增加前缀路径
- PrefixPath=/prefix
重试过滤器
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
过滤器设置请求大小
filters:
- name: RequestSize #过滤器名称
args: #控制请求的大小,超过则返回413.请求最大默认5000000 约5M
maxSize:1000 #1KB
spring-session
只有当集成Spring Session才会将session放到Redis,来实现共享Session功能,如果项目继承了Spring Session中的Spring Security框架,如果希望安全验证信息可以转发到远程应用,那么这个配置是必须的。
filters:
- SaveSession
默认filter
spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-Red, Default-Blue
- PrefixPath=/httpbin
边栏推荐
- 187. Repeated DNA sequence - with unordered_ Map basic content
- Limited query of common SQL operations
- A tab Sina navigation bar
- Cut! 39 year old Ali P9, saved 150million
- 丸子百度小程序详细配置教程,审核通过。
- Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
- Summary and practice of knowledge map construction technology
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- Hmi-31- [motion mode] solve the problem of picture display of music module
- Learn game model 3D characters, come out to find a job?
猜你喜欢
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
Avoid material "minefields"! Play with super high conversion rate
Action News
Stored procedure and stored function in Oracle
丸子百度小程序详细配置教程,审核通过。
Hmi-32- [motion mode] add light panel and basic information column
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
Official announcement! The third cloud native programming challenge is officially launched!
The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
随机推荐
Talk about the things that must be paid attention to when interviewing programmers
openresty ngx_ Lua variable operation
CAM Pytorch
Traditional chips and AI chips
如何搭建一支搞垮公司的技术团队?
如何搭建一支搞垮公司的技術團隊?
Pytest (4) - test case execution sequence
Action News
Timescaledb 2.5.2 release, time series database based on PostgreSQL
Restful fast request 2022.2.1 release, support curl import
低度酒赛道进入洗牌期,新品牌如何破局三大难题?
Limited query of common SQL operations
Li Kou Jianzhi offer -- binary tree chapter
Unpool(nn.MaxUnpool2d)
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
Exploration of short text analysis in the field of medical and health (I)
Android advanced interview question record in 2022
Which common ports should the server open
Leetcode takes out the least number of magic beans
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation