当前位置:网站首页>【微服务|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
边栏推荐
- Asynchronous and promise
- Erreur de type de datagramme MySQL en utilisant Druid
- A tab Sina navigation bar
- Yolov5 model training and detection
- [机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
- Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
- Tla+ through examples (XI) -- propositional logic and examples
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- Practical case of SQL optimization: speed up your database
猜你喜欢

Traditional chips and AI chips

The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by

The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
![Hmi-32- [motion mode] add light panel and basic information column](/img/9c/0b25c0a41758652848aed2a269880f.jpg)
Hmi-32- [motion mode] add light panel and basic information column

Official announcement! The third cloud native programming challenge is officially launched!

Application and Optimization Practice of redis in vivo push platform

Tucson will lose more than $400million in the next year

Introduce reflow & repaint, and how to optimize it?

A tab Sina navigation bar
![Hmi-31- [motion mode] solve the problem of picture display of music module](/img/9c/0b25c0a41758652848aed2a269880f.jpg)
Hmi-31- [motion mode] solve the problem of picture display of music module
随机推荐
179. Maximum number - sort
Uniapp navigateto jump failure
[technology development-26]: data security of new information and communication networks
Application and Optimization Practice of redis in vivo push platform
Visual explanation of Newton iteration method
100 basic multiple choice questions of C language (with answers) 04
2022/02/13
Character painting, I use characters to draw a Bing Dwen Dwen
Bert fine tuning skills experiment
Pytest (5) - assertion
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
Single line function*
Grub 2.12 will be released this year to continue to improve boot security
Exploration of short text analysis in the field of medical and health (II)
I use these six code comparison tools
RichView TRVStyle MainRVStyle
Three properties that a good homomorphic encryption should satisfy
[机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”