@@ -322,44 +322,77 @@ build a new `WebClient`, based on, but without affecting the current instance:
322
322
323
323
324
324
[[webflux-client-filter]]
325
- == Filters
325
+ == Client Filters
326
326
327
- `WebClient` supports interception style request filtering:
327
+ You can register an `ExchangeFilterFunction` in the `WebClient.Builder` to intercept and
328
+ possibly modify requests performed through the client:
328
329
329
330
[source,java,intent=0]
330
331
[subs="verbatim,quotes"]
331
332
----
332
- WebClient client = WebClient.builder()
333
- .filter((request, next) -> {
334
- ClientRequest filtered = ClientRequest.from(request)
335
- .header("foo", "bar")
336
- .build();
337
- return next.exchange(filtered);
338
- })
339
- .build();
333
+ WebClient client = WebClient.builder()
334
+ .filter((request, next) -> {
335
+
336
+ ClientRequest filtered = ClientRequest.from(request)
337
+ .header("foo", "bar")
338
+ .build();
339
+
340
+ return next.exchange(filtered);
341
+ })
342
+ .build();
340
343
----
341
344
342
- `ExchangeFilterFunctions` provides a filter for basic authentication:
345
+ This can be used for cross-cutting concerns such as authentication. The example below uses
346
+ a filter for basic authentication through a static factory method:
343
347
344
348
[source,java,intent=0]
345
349
[subs="verbatim,quotes"]
346
350
----
347
351
348
- // static import of ExchangeFilterFunctions.basicAuthentication
352
+ // static import of ExchangeFilterFunctions.basicAuthentication
349
353
350
- WebClient client = WebClient.builder()
351
- .filter(basicAuthentication("user", "pwd "))
352
- .build();
354
+ WebClient client = WebClient.builder()
355
+ .filter(basicAuthentication("user", "password "))
356
+ .build();
353
357
----
354
358
355
- You can also mutate an existing `WebClient` instance without affecting the original:
359
+ Filters apply globally to every request. To change how a filter's behavior for a specific
360
+ request, you can add request attributes to the `ClientRequest` that can then be accessed
361
+ by all filters in the chain:
356
362
357
363
[source,java,intent=0]
358
364
[subs="verbatim,quotes"]
359
365
----
360
- WebClient filteredClient = client.mutate()
361
- .filter(basicAuthentication("user", "pwd")
362
- .build();
366
+ WebClient client = WebClient.builder()
367
+ .filter((request, next) -> {
368
+ Optional<Object> usr = request.attribute("myAttribute");
369
+ // ...
370
+ })
371
+ .build();
372
+
373
+ client.get().uri("http://example.org/")
374
+ .attribute("myAttribute", "...")
375
+ .retrieve()
376
+ .bodyToMono(Void.class);
377
+
378
+ }
379
+ ----
380
+
381
+ You can also replicate an existing `WebClient`, and insert new filters or remove already
382
+ registered filters. In the example below, a basic authentication filter is inserted at
383
+ index 0:
384
+
385
+ [source,java,intent=0]
386
+ [subs="verbatim,quotes"]
387
+ ----
388
+
389
+ // static import of ExchangeFilterFunctions.basicAuthentication
390
+
391
+ WebClient client = webClient.mutate()
392
+ .filters(filterList -> {
393
+ filterList.add(0, basicAuthentication("user", "password"));
394
+ })
395
+ .build();
363
396
----
364
397
365
398
0 commit comments