Merge changes from 2.0.x.

This commit is contained in:
Olga Maciaszek-Sharma
2019-02-13 16:14:20 +01:00
2 changed files with 33 additions and 1 deletions

View File

@@ -1584,7 +1584,38 @@ public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory<PostG
=== Writing Custom Global Filters
TODO: document writing Custom Global Filters
In order to write a custom global filter, you will need to implement `GlobalFilter` interface. This will apply the filter to all requests.
Example of how to set up a Global Pre and Post filter, respectively
[source,java]
----
@Bean
public GlobalFilter customGlobalFilter() {
return (exchange, chain) -> 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

View File

@@ -12,6 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.gateway.config;