@@ -1698,6 +1698,52 @@ NOTE: The default implementation of `ReactiveOAuth2AuthorizedClientService` used
|
||||
uses an in-memory data store. You will need to provide your own implementation `ReactiveOAuth2AuthorizedClientService`
|
||||
if you need a more robust solution.
|
||||
|
||||
|
||||
=== The `CacheRequestBody` `GatewayFilter` Factory
|
||||
There are certain situation need to read body.Since the request body stream can only be read once, we need to cache the request body.
|
||||
You can use the `CacheRequestBody` filter to cache request body before it send to the downstream and get body from exchagne attribute.
|
||||
|
||||
|
||||
The following listing shows how to cache the request body `GatewayFilter`:
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
public RouteLocator routes(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
.route("cache_request_body_route", r -> r.path("/downstream/**")
|
||||
.filters(f -> f.prefixPath("/httpbin")
|
||||
.cacheRequestBody(String.class).uri(uri))
|
||||
.build();
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
|
||||
.application.yml
|
||||
====
|
||||
[source,yaml]
|
||||
----
|
||||
spring:
|
||||
cloud:
|
||||
gateway:
|
||||
routes:
|
||||
- id: cache_request_body_route
|
||||
uri: lb://downstream
|
||||
predicates:
|
||||
- Path=/downstream/**
|
||||
filters:
|
||||
- name: CacheRequestBody
|
||||
args:
|
||||
bodyClass: java.lang.String
|
||||
----
|
||||
====
|
||||
|
||||
`CacheRequestBody` will extract request body and conver it to body class (such as `java.lang.String`, defined in the preceding example). then places it in the `ServerWebExchange.getAttributes()` with a key defined in `ServerWebExchangeUtils.CACHED_REQUEST_BODY_ATTR`.
|
||||
|
||||
NOTE: This filter only works with http request (including https).
|
||||
|
||||
=== Default Filters
|
||||
|
||||
To add a filter and apply it to all routes, you can use `spring.cloud.gateway.default-filters`.
|
||||
|
||||
Reference in New Issue
Block a user