From 8fd35c55b1e2175d0cf77f83394fed52a26aeb2f Mon Sep 17 00:00:00 2001 From: Alex Simons Date: Wed, 13 Feb 2019 09:05:19 -0600 Subject: [PATCH] Revisited documentation about writing a custom global pre/post-filter (#836) * Added documentation about writing a custom global pre/post-filter * Removed unnecessary words about GlobalFilter. * Added back removed section title --- .../main/asciidoc/spring-cloud-gateway.adoc | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index dd397b13c..f7d731d16 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -1387,7 +1387,38 @@ public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory exchange.getPrincipal() + .map(Principal::getName) + .defaultIfEmpty("Default User") + .map(userName -> { + //adds header to proxied request + exchange.getRequest().mutate().header("CUSTOM-REQUEST-HEADER", userName).build(); + return exchange; + }) + .flatMap(chain::filter); +} + +@Bean +public GlobalFilter customGlobalPostFilter() { + return (exchange, chain) -> chain.filter(exchange) + .then(Mono.just(exchange)) + .map(serverWebExchange -> { + //adds header to response + serverWebExchange.getResponse().getHeaders().set("CUSTOM-RESPONSE-HEADER", + HttpStatus.OK.equals(serverWebExchange.getResponse().getStatusCode()) ? "It worked": "It did not work"); + return serverWebExchange; + }) + .then(); +} +--- === Writing Custom Route Locators and Writers