diff --git a/2.1.x/multi/multi__actuator_api.html b/2.1.x/multi/multi__actuator_api.html index 62795a94..a6bf43b9 100644 --- a/2.1.x/multi/multi__actuator_api.html +++ b/2.1.x/multi/multi__actuator_api.html @@ -1,9 +1,9 @@ - 11. Actuator API

11. Actuator API

The /gateway actuator endpoint allows to monitor and interact with a Spring Cloud Gateway application. To be remotely accessible, the endpoint has to be enabled and exposed via HTTP or JMX in the application properties.

application.properties.  + 13. Actuator API

13. Actuator API

The /gateway actuator endpoint allows to monitor and interact with a Spring Cloud Gateway application. To be remotely accessible, the endpoint has to be enabled and exposed via HTTP or JMX in the application properties.

application.properties. 

management.endpoint.gateway.enabled=true # default value
 management.endpoints.web.exposure.include=gateway

-

11.1 Verbose Actuator Format

A new, more verbose format has been added to Gateway. This adds more detail to each route allowing to view the predicates and filters associated to each route along with any configuration that is available.

/actuator/gateway/routes

[
+

13.1 Verbose Actuator Format

A new, more verbose format has been added to Gateway. This adds more detail to each route allowing to view the predicates and filters associated to each route along with any configuration that is available.

/actuator/gateway/routes

[
   {
     "predicate": "(Hosts: [**.addrequestheader.org] && Paths: [/headers], match trailing slash: true)",
     "route_id": "add_request_header_test",
@@ -17,7 +17,7 @@
   }
 ]

To enable this feature, set the following property:

application.properties. 

spring.cloud.gateway.actuator.verbose.enabled=true

-

This will default to true in a future release.

11.2 Retrieving route filters

11.2.1 Global Filters

To retrieve the global filters applied to all routes, make a GET request to /actuator/gateway/globalfilters. The resulting response is similar to the following:

{
+

This will default to true in a future release.

13.2 Retrieving route filters

13.2.1 Global Filters

To retrieve the global filters applied to all routes, make a GET request to /actuator/gateway/globalfilters. The resulting response is similar to the following:

{
   "org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5": 10100,
   "org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@4f6fd101": 10000,
   "org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@32d22650": -1,
@@ -26,11 +26,11 @@
   "org.springframework.cloud.gateway.filter.ForwardPathFilter@33a71d23": 0,
   "org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@135064ea": 2147483637,
   "org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@23c05889": 2147483646
-}

The response contains details of the global filters in place. For each global filter is provided the string representation of the filter object (e.g., org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5) and the corresponding order in the filter chain.

11.2.2 Route Filters

To retrieve the GatewayFilter factories applied to routes, make a GET request to /actuator/gateway/routefilters. The resulting response is similar to the following:

{
+}

The response contains details of the global filters in place. For each global filter is provided the string representation of the filter object (e.g., org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5) and the corresponding order in the filter chain.

13.2.2 Route Filters

To retrieve the GatewayFilter factories applied to routes, make a GET request to /actuator/gateway/routefilters. The resulting response is similar to the following:

{
   "[AddRequestHeaderGatewayFilterFactory@570ed9c configClass = AbstractNameValueGatewayFilterFactory.NameValueConfig]": null,
   "[SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]": null,
   "[SaveSessionGatewayFilterFactory@4449b273 configClass = Object]": null
-}

The response contains details of the GatewayFilter factories applied to any particular route. For each factory is provided the string representation of the corresponding object (e.g., [SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]). Note that the null value is due to an incomplete implementation of the endpoint controller, for that it tries to set the order of the object in the filter chain, which does not apply to a GatewayFilter factory object.

11.3 Refreshing the route cache

To clear the routes cache, make a POST request to /actuator/gateway/refresh. The request returns a 200 without response body.

11.4 Retrieving the routes defined in the gateway

To retrieve the routes defined in the gateway, make a GET request to /actuator/gateway/routes. The resulting response is similar to the following:

[{
+}

The response contains details of the GatewayFilter factories applied to any particular route. For each factory is provided the string representation of the corresponding object (e.g., [SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]). Note that the null value is due to an incomplete implementation of the endpoint controller, for that it tries to set the order of the object in the filter chain, which does not apply to a GatewayFilter factory object.

13.3 Refreshing the route cache

To clear the routes cache, make a POST request to /actuator/gateway/refresh. The request returns a 200 without response body.

13.4 Retrieving the routes defined in the gateway

To retrieve the routes defined in the gateway, make a GET request to /actuator/gateway/routes. The resulting response is similar to the following:

[{
   "route_id": "first_route",
   "route_object": {
     "predicate": "org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$432/1736826640@1e9d7e7d",
@@ -47,7 +47,7 @@
     "filters": []
   },
   "order": 0
-}]

The response contains details of all the routes defined in the gateway. The following table describes the structure of each element (i.e., a route) of the response.

PathTypeDescription

route_id

String

The route id.

route_object.predicate

Object

The route predicate.

route_object.filters

Array

The GatewayFilter factories applied to the route.

order

Number

The route order.

11.5 Retrieving information about a particular route

To retrieve information about a single route, make a GET request to /actuator/gateway/routes/{id} (e.g., /actuator/gateway/routes/first_route). The resulting response is similar to the following:

{
+}]

The response contains details of all the routes defined in the gateway. The following table describes the structure of each element (i.e., a route) of the response.

PathTypeDescription

route_id

String

The route id.

route_object.predicate

Object

The route predicate.

route_object.filters

Array

The GatewayFilter factories applied to the route.

order

Number

The route order.

13.5 Retrieving information about a particular route

To retrieve information about a single route, make a GET request to /actuator/gateway/routes/{id} (e.g., /actuator/gateway/routes/first_route). The resulting response is similar to the following:

{
   "id": "first_route",
   "predicates": [{
     "name": "Path",
@@ -56,4 +56,4 @@
   "filters": [],
   "uri": "https://www.uri-destination.org",
   "order": 0
-}]

The following table describes the structure of the response.

PathTypeDescription

id

String

The route id.

predicates

Array

The collection of route predicates. Each item defines the name and the arguments of a given predicate.

filters

Array

The collection of filters applied to the route.

uri

String

The destination URI of the route.

order

Number

The route order.

11.6 Creating and deleting a particular route

To create a route, make a POST request to /gateway/routes/{id_route_to_create} with a JSON body that specifies the fields of the route (see the previous subsection).

To delete a route, make a DELETE request to /gateway/routes/{id_route_to_delete}.

11.7 Recap: list of all endpoints

The table below summarises the Spring Cloud Gateway actuator endpoints. Note that each endpoint has /actuator/gateway as the base-path.

IDHTTP MethodDescription

globalfilters

GET

Displays the list of global filters applied to the routes.

routefilters

GET

Displays the list of GatewayFilter factories applied to a particular route.

refresh

POST

Clears the routes cache.

routes

GET

Displays the list of routes defined in the gateway.

routes/{id}

GET

Displays information about a particular route.

routes/{id}

POST

Add a new route to the gateway.

routes/{id}

DELETE

Remove an existing route from the gateway.

\ No newline at end of file +}]

The following table describes the structure of the response.

PathTypeDescription

id

String

The route id.

predicates

Array

The collection of route predicates. Each item defines the name and the arguments of a given predicate.

filters

Array

The collection of filters applied to the route.

uri

String

The destination URI of the route.

order

Number

The route order.

13.6 Creating and deleting a particular route

To create a route, make a POST request to /gateway/routes/{id_route_to_create} with a JSON body that specifies the fields of the route (see the previous subsection).

To delete a route, make a DELETE request to /gateway/routes/{id_route_to_delete}.

13.7 Recap: list of all endpoints

The table below summarises the Spring Cloud Gateway actuator endpoints. Note that each endpoint has /actuator/gateway as the base-path.

IDHTTP MethodDescription

globalfilters

GET

Displays the list of global filters applied to the routes.

routefilters

GET

Displays the list of GatewayFilter factories applied to a particular route.

refresh

POST

Clears the routes cache.

routes

GET

Displays the list of routes defined in the gateway.

routes/{id}

GET

Displays information about a particular route.

routes/{id}

POST

Add a new route to the gateway.

routes/{id}

DELETE

Remove an existing route from the gateway.

\ No newline at end of file diff --git a/2.1.x/multi/multi__building_a_simple_gateway_using_spring_mvc_or_webflux.html b/2.1.x/multi/multi__building_a_simple_gateway_using_spring_mvc_or_webflux.html index f3fcd961..14bb9f43 100644 --- a/2.1.x/multi/multi__building_a_simple_gateway_using_spring_mvc_or_webflux.html +++ b/2.1.x/multi/multi__building_a_simple_gateway_using_spring_mvc_or_webflux.html @@ -1,6 +1,6 @@ - 14. Building a Simple Gateway Using Spring MVC or Webflux

14. Building a Simple Gateway Using Spring MVC or Webflux

Spring Cloud Gateway provides a utility object called ProxyExchange which you can use inside a regular Spring web handler as a method parameter. It supports basic downstream HTTP exchanges via methods that mirror the HTTP verbs. With MVC it also supports forwarding to a local handler via the forward() method. To use the ProxyExchange just include the right module in your classpath (either spring-cloud-gateway-mvc or spring-cloud-gateway-webflux).

MVC example (proxying a request to "/test" downstream to a remote server):

@RestController
+   16. Building a Simple Gateway Using Spring MVC or Webflux

16. Building a Simple Gateway Using Spring MVC or Webflux

[Warning]Warning

The following describes an alternative style gateway. None of the prior documentation applies to what follows.

Spring Cloud Gateway provides a utility object called ProxyExchange which you can use inside a regular Spring web handler as a method parameter. It supports basic downstream HTTP exchanges via methods that mirror the HTTP verbs. With MVC it also supports forwarding to a local handler via the forward() method. To use the ProxyExchange just include the right module in your classpath (either spring-cloud-gateway-mvc or spring-cloud-gateway-webflux).

MVC example (proxying a request to "/test" downstream to a remote server):

@RestController
 @SpringBootApplication
 public class GatewaySampleApplication {
 
@@ -28,4 +28,4 @@
 public ResponseEntity<?> proxyPath(ProxyExchange<byte[]> proxy) throws Exception {
   String path = proxy.path("/proxy/path/");
   return proxy.uri(home.toString() + "/foos/" + path).get();
-}

All the features of Spring MVC or Webflux are available to Gateway handler methods. So you can inject request headers and query parameters, for instance, and you can constrain the incoming requests with declarations in the mapping annotation. See the documentation for @RequestMapping in Spring MVC for more details of those features.

Headers can be added to the downstream response using the header() methods on ProxyExchange.

You can also manipulate response headers (and anything else you like in the response) by adding a mapper to the get() etc. method. The mapper is a Function that takes the incoming ResponseEntity and converts it to an outgoing one.

First class support is provided for "sensitive" headers ("cookie" and "authorization" by default) which are not passed downstream, and for "proxy" headers (x-forwarded-*).

\ No newline at end of file +}

All the features of Spring MVC or Webflux are available to Gateway handler methods. So you can inject request headers and query parameters, for instance, and you can constrain the incoming requests with declarations in the mapping annotation. See the documentation for @RequestMapping in Spring MVC for more details of those features.

Headers can be added to the downstream response using the header() methods on ProxyExchange.

You can also manipulate response headers (and anything else you like in the response) by adding a mapper to the get() etc. method. The mapper is a Function that takes the incoming ResponseEntity and converts it to an outgoing one.

First class support is provided for "sensitive" headers ("cookie" and "authorization" by default) which are not passed downstream, and for "proxy" headers (x-forwarded-*).

\ No newline at end of file diff --git a/2.1.x/multi/multi__configuration.html b/2.1.x/multi/multi__configuration.html index 43383749..fdaceac0 100644 --- a/2.1.x/multi/multi__configuration.html +++ b/2.1.x/multi/multi__configuration.html @@ -1,6 +1,6 @@ - 8. Configuration

8. Configuration

Configuration for Spring Cloud Gateway is driven by a collection of RouteDefinitionLocators.

RouteDefinitionLocator.java.  + 10. Configuration

10. Configuration

Configuration for Spring Cloud Gateway is driven by a collection of RouteDefinitionLocators.

RouteDefinitionLocator.java. 

public interface RouteDefinitionLocator {
 	Flux<RouteDefinition> getRouteDefinitions();
 }

@@ -19,7 +19,7 @@ uri: https://example.org filters: - SetStatus=401

-

For some usages of the gateway, properties will be adequate, but some production use cases will benefit from loading configuration from an external source, such as a database. Future milestone versions will have RouteDefinitionLocator implementations based off of Spring Data Repositories such as: Redis, MongoDB and Cassandra.

8.1 Fluent Java Routes API

To allow for simple configuration in Java, there is a fluent API defined in the RouteLocatorBuilder bean.

GatewaySampleApplication.java.  +

For some usages of the gateway, properties will be adequate, but some production use cases will benefit from loading configuration from an external source, such as a database. Future milestone versions will have RouteDefinitionLocator implementations based off of Spring Data Repositories such as: Redis, MongoDB and Cassandra.

10.1 Fluent Java Routes API

To allow for simple configuration in Java, there is a fluent API defined in the RouteLocatorBuilder bean.

GatewaySampleApplication.java. 

// static imports from GatewayFilters and RoutePredicates
 @Bean
 public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
@@ -44,7 +44,7 @@
             )
             .build();
 }

-

This style also allows for more custom predicate assertions. The predicates defined by RouteDefinitionLocator beans are combined using logical and. By using the fluent Java API, you can use the and(), or() and negate() operators on the Predicate class.

8.2 DiscoveryClient Route Definition Locator

The Gateway can be configured to create routes based on services registered with a DiscoveryClient compatible service registry.

To enable this, set spring.cloud.gateway.discovery.locator.enabled=true and make sure a DiscoveryClient implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper).

8.2.1 Configuring Predicates and Filters For DiscoveryClient Routes

By default the Gateway defines a single predicate and filter for routes created via a DiscoveryClient.

The default predicate is a path predicate defined with the pattern /serviceId/**, where serviceId is +

This style also allows for more custom predicate assertions. The predicates defined by RouteDefinitionLocator beans are combined using logical and. By using the fluent Java API, you can use the and(), or() and negate() operators on the Predicate class.

10.2 DiscoveryClient Route Definition Locator

The Gateway can be configured to create routes based on services registered with a DiscoveryClient compatible service registry.

To enable this, set spring.cloud.gateway.discovery.locator.enabled=true and make sure a DiscoveryClient implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper).

10.2.1 Configuring Predicates and Filters For DiscoveryClient Routes

By default the Gateway defines a single predicate and filter for routes created via a DiscoveryClient.

The default predicate is a path predicate defined with the pattern /serviceId/**, where serviceId is the id of the service from the DiscoveryClient.

The default filter is rewrite path filter with the regex /serviceId/(?<remaining>.*) and the replacement /${remaining}. This just strips the service id from the path before the request is sent downstream.

If you would like to customize the predicates and/or filters used by the DiscoveryClient routes you can do so @@ -60,4 +60,4 @@ spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'" spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"

-

\ No newline at end of file +

\ No newline at end of file diff --git a/2.1.x/multi/multi__configuring_route_predicate_factories_and_gateway_filter_factories.html b/2.1.x/multi/multi__configuring_route_predicate_factories_and_gateway_filter_factories.html new file mode 100644 index 00000000..6df60fb2 --- /dev/null +++ b/2.1.x/multi/multi__configuring_route_predicate_factories_and_gateway_filter_factories.html @@ -0,0 +1,24 @@ + + + 4. Configuring Route Predicate Factories and Gateway Filter Factories

4. Configuring Route Predicate Factories and Gateway Filter Factories

There are two ways to configure predicates and filters: shortcuts and fully expanded arguments. Most examples below use the shortcut way.

The name and argument names will be listed as code in the first sentance or two of the each section. The arguments are typically listed in the order that would be needed for the shortcut configuration.

4.1 Shortcut Configuration

Shortcut configuration is recognized by the filter name, followed by an equals sign (=), followed by argument values separated by commas (,).

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: after_route
+        uri: https://example.org
+        predicates:
+        - Cookie=mycookie,mycookievalue

+

The previous sample defines the Cookie Route Predicate Factory with two arguments, the cookie name, mycookie and the value to match mycookievalue.

4.2 Fully Expanded Arguments

Fully expanded arguments appear more like standard yaml configuration with name/value pairs. Typically, there will be a name key and an args key. The args key is a map of key value pairs to configure the predicate or filter.

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: after_route
+        uri: https://example.org
+        predicates:
+        - name: Cookie
+          args:
+            name: mycookie
+            regexp: mycookievalue

+

This is the full configuration of the shortcut configuration of the Cookie predicate shown above.

\ No newline at end of file diff --git a/2.1.x/multi/multi__cors_configuration.html b/2.1.x/multi/multi__cors_configuration.html index d5b22845..dd03e749 100644 --- a/2.1.x/multi/multi__cors_configuration.html +++ b/2.1.x/multi/multi__cors_configuration.html @@ -1,6 +1,6 @@ - 10. CORS Configuration

10. CORS Configuration

The gateway can be configured to control CORS behavior. The "global" CORS configuration is a map of URL patterns to Spring Framework CorsConfiguration.

application.yml.  + 12. CORS Configuration

12. CORS Configuration

The gateway can be configured to control CORS behavior. The "global" CORS configuration is a map of URL patterns to Spring Framework CorsConfiguration.

application.yml. 

spring:
   cloud:
     gateway:
@@ -10,4 +10,4 @@
             allowedOrigins: "https://docs.spring.io"
             allowedMethods:
             - GET

-

In the example above, CORS requests will be allowed from requests that originate from docs.spring.io for all GET requested paths.

To provide the same CORS configuration to requests that are not handled by some gateway route predicate, set the property spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping equal to true. This is useful when trying to support CORS preflight requests and your route predicate doesn’t evalute to true because the http method is options.

\ No newline at end of file +

In the example above, CORS requests will be allowed from requests that originate from docs.spring.io for all GET requested paths.

To provide the same CORS configuration to requests that are not handled by some gateway route predicate, set the property spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping equal to true. This is useful when trying to support CORS preflight requests and your route predicate doesn’t evalute to true because the http method is options.

\ No newline at end of file diff --git a/2.1.x/multi/multi__developer_guide.html b/2.1.x/multi/multi__developer_guide.html index c4e55f08..c431589b 100644 --- a/2.1.x/multi/multi__developer_guide.html +++ b/2.1.x/multi/multi__developer_guide.html @@ -1,6 +1,30 @@ - 13. Developer Guide

13. Developer Guide

TODO: overview of writing custom integrations

13.1 Writing Custom Route Predicate Factories

TODO: document writing Custom Route Predicate Factories

13.2 Writing Custom GatewayFilter Factories

In order to write a GatewayFilter you will need to implement GatewayFilterFactory. There is an abstract class called AbstractGatewayFilterFactory which you can extend.

PreGatewayFilterFactory.java.  + 15. Developer Guide

15. Developer Guide

These are basic guides to writing some custom components of the gateway.

15.1 Writing Custom Route Predicate Factories

In order to write a Route Predicate you will need to implement RoutePredicateFactory. There is an abstract class called AbstractRoutePredicateFactory which you can extend.

MyRoutePredicateFactory.java.  +

public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<HeaderRoutePredicateFactory.Config> {
+
+    public MyRoutePredicateFactory() {
+        super(Config.class);
+    }
+
+    @Override
+    public Predicate<ServerWebExchange> apply(Config config) {
+        // grab configuration from Config object
+        return exchange -> {
+            //grab the request
+            ServerHttpRequest request = exchange.getRequest();
+            //take information from the request to see if it
+            //matches configuration.
+            return matches(config, request);
+        };
+    }
+
+    public static class Config {
+        //Put the configuration properties for your filter here
+    }
+
+}

+

15.2 Writing Custom GatewayFilter Factories

In order to write a GatewayFilter you will need to implement GatewayFilterFactory. There is an abstract class called AbstractGatewayFilterFactory which you can extend.

PreGatewayFilterFactory.java. 

public class PreGatewayFilterFactory extends AbstractGatewayFilterFactory<PreGatewayFilterFactory.Config> {
 
 	public PreGatewayFilterFactory() {
@@ -11,16 +35,16 @@
 	public GatewayFilter apply(Config config) {
 		// grab configuration from Config object
 		return (exchange, chain) -> {
-            //If you want to build a "pre" filter you need to manipulate the
-            //request before calling chain.filter
-            ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
-            //use builder to manipulate the request
-            return chain.filter(exchange.mutate().request(request).build());
+			//If you want to build a "pre" filter you need to manipulate the
+			//request before calling chain.filter
+			ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
+			//use builder to manipulate the request
+			return chain.filter(exchange.mutate().request(request).build());
 		};
 	}
 
 	public static class Config {
-        //Put the configuration properties for your filter here
+		//Put the configuration properties for your filter here
 	}
 
 }

@@ -43,11 +67,11 @@ } public static class Config { - //Put the configuration properties for your filter here + //Put the configuration properties for your filter here } }

-

13.3 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

@Bean
+

15.3 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

@Bean
 public GlobalFilter customGlobalFilter() {
     return (exchange, chain) -> exchange.getPrincipal()
         .map(Principal::getName)
@@ -71,4 +95,4 @@
           return serverWebExchange;
         })
         .then();
-}

13.4 Writing Custom Route Locators and Writers

TODO: document writing Custom Route Locators and Writers

\ No newline at end of file +}
\ No newline at end of file diff --git a/2.1.x/multi/multi__gatewayfilter_factories.html b/2.1.x/multi/multi__gatewayfilter_factories.html index 832887c9..b05a4826 100644 --- a/2.1.x/multi/multi__gatewayfilter_factories.html +++ b/2.1.x/multi/multi__gatewayfilter_factories.html @@ -1,6 +1,6 @@ - 5. GatewayFilter Factories

5. GatewayFilter Factories

Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories.

NOTE For more detailed examples on how to use any of the following filters, take a look at the unit tests.

5.1 AddRequestHeader GatewayFilter Factory

The AddRequestHeader GatewayFilter Factory takes a name and value parameter.

application.yml.  + 6. GatewayFilter Factories

6. GatewayFilter Factories

Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories.

NOTE For more detailed examples on how to use any of the following filters, take a look at the unit tests.

6.1 AddRequestHeader GatewayFilter Factory

The AddRequestHeader GatewayFilter Factory takes a name and value parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -20,7 +20,7 @@
         - Path=/foo/{segment}
         filters:
         - AddRequestHeader=X-Request-Foo, Bar-{segment}

-

5.2 AddRequestParameter GatewayFilter Factory

The AddRequestParameter GatewayFilter Factory takes a name and value parameter.

application.yml.  +

6.2 AddRequestParameter GatewayFilter Factory

The AddRequestParameter GatewayFilter Factory takes a name and value parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -40,7 +40,7 @@
         - Host: {segment}.myhost.org
         filters:
         - AddRequestParameter=foo, bar-{segment}

-

5.3 AddResponseHeader GatewayFilter Factory

The AddResponseHeader GatewayFilter Factory takes a name and value parameter.

application.yml.  +

6.3 AddResponseHeader GatewayFilter Factory

The AddResponseHeader GatewayFilter Factory takes a name and value parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -60,7 +60,7 @@
         - Host: {segment}.myhost.org
         filters:
         - AddResponseHeader=foo, bar-{segment}

-

5.4 DedupeResponseHeader GatewayFilter Factory

The DedupeResponseHeader GatewayFilter Factory takes a name parameter and an optional strategy parameter. name can contain a list of header names, space separated.

application.yml.  +

6.4 DedupeResponseHeader GatewayFilter Factory

The DedupeResponseHeader GatewayFilter Factory takes a name parameter and an optional strategy parameter. name can contain a list of header names, space separated.

application.yml. 

spring:
   cloud:
     gateway:
@@ -69,8 +69,8 @@
         uri: https://example.org
         filters:
         - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin

-

This will remove duplicate values of Access-Control-Allow-Credentials and Access-Control-Allow-Origin response headers in cases when both the gateway CORS logic and the downstream add them.

The DedupeResponseHeader filter also accepts an optional strategy parameter. The accepted values are RETAIN_FIRST (default), RETAIN_LAST, and RETAIN_UNIQUE.

5.5 Hystrix GatewayFilter Factory

Hystrix is a library from Netflix that implements the circuit breaker pattern. -The Hystrix GatewayFilter allows you to introduce circuit breakers to your gateway routes, protecting your services from cascading failures and allowing you to provide fallback responses in the event of downstream failures.

To enable Hystrix GatewayFilters in your project, add a dependency on spring-cloud-starter-netflix-hystrix from Spring Cloud Netflix.

The Hystrix GatewayFilter Factory requires a single name parameter, which is the name of the HystrixCommand.

application.yml.  +

This will remove duplicate values of Access-Control-Allow-Credentials and Access-Control-Allow-Origin response headers in cases when both the gateway CORS logic and the downstream add them.

The DedupeResponseHeader filter also accepts an optional strategy parameter. The accepted values are RETAIN_FIRST (default), RETAIN_LAST, and RETAIN_UNIQUE.

6.5 Hystrix GatewayFilter Factory

Hystrix is a library from Netflix that implements the circuit breaker pattern. +The Hystrix GatewayFilter allows you to introduce circuit breakers to your gateway routes, protecting your services from cascading failures and allowing you to provide fallback responses in the event of downstream failures.

To enable Hystrix GatewayFilters in your project, add a dependency on spring-cloud-starter-netflix-hystrix from Spring Cloud Netflix.

The Hystrix GatewayFilter Factory requires a single name parameter, which is the name of the HystrixCommand.

application.yml. 

spring:
   cloud:
     gateway:
@@ -118,9 +118,9 @@ app, registered under ServerWebExchange as the
 ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR attribute that can be used when
 handling the fallback within the gateway app.

For the external controller/ handler scenario, headers can be added with exception details. You can find more information -on it in the FallbackHeaders GatewayFilter Factory section.

Hystrix settings (such as timeouts) can be configured with global defaults or on a route by route basis using application properties as explained on the Hystrix wiki.

To set a 5 second timeout for the example route above, the following configuration would be used:

application.yml.  +on it in the FallbackHeaders GatewayFilter Factory section.

Hystrix settings (such as timeouts) can be configured with global defaults or on a route by route basis using application properties as explained on the Hystrix wiki.

To set a 5 second timeout for the example route above, the following configuration would be used:

application.yml. 

hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000

-

5.6 FallbackHeaders GatewayFilter Factory

The FallbackHeaders factory allows you to add Hystrix execution exception details in headers of a request forwarded to +

6.6 FallbackHeaders GatewayFilter Factory

The FallbackHeaders factory allows you to add Hystrix execution exception details in headers of a request forwarded to a fallbackUri in an external application, like in the following scenario:

application.yml. 

spring:
   cloud:
@@ -146,7 +146,7 @@ a fallbackUri in an external application, like in t
 

In this example, after an execution exception occurs while running the HystrixCommand, the request will be forwarde to the fallback endpoint or handler in an app running on localhost:9994. The headers with the exception type, message and -if available- root cause exception type and message will be added to that request by the FallbackHeaders filter.

The names of the headers can be overwritten in the config by setting the values of the arguments listed below, along with -their default values:

  • executionExceptionTypeHeaderName ("Execution-Exception-Type")
  • executionExceptionMessageHeaderName ("Execution-Exception-Message")
  • rootCauseExceptionTypeHeaderName ("Root-Cause-Exception-Type")
  • rootCauseExceptionMessageHeaderName ("Root-Cause-Exception-Message")

You can find more information on how Hystrix works with Gateway in the Hystrix GatewayFilter Factory section.

5.7 MapRequestHeader GatewayFilter Factory

The MapRequestHeader GatewayFilter Factory takes 'fromHeader' and 'toHeader' parameters. It creates a new named header (toHeader) and the value is extracted out of an existing named header (fromHeader) from the incoming http request. If the input header does not exist then the filter has no impact. If the new named header already exists then it’s values will be augmented with the new values.

application.yml.  +their default values:

  • executionExceptionTypeHeaderName ("Execution-Exception-Type")
  • executionExceptionMessageHeaderName ("Execution-Exception-Message")
  • rootCauseExceptionTypeHeaderName ("Root-Cause-Exception-Type")
  • rootCauseExceptionMessageHeaderName ("Root-Cause-Exception-Message")

You can find more information on how Hystrix works with Gateway in the Hystrix GatewayFilter Factory section.

6.7 MapRequestHeader GatewayFilter Factory

The MapRequestHeader GatewayFilter Facstory takes 'fromHeader' and 'toHeader' parameters. It creates a new named header (toHeader) and the value is extracted out of an existing named header (fromHeader) from the incoming http request. If the input header does not exist then the filter has no impact. If the new named header already exists then it’s values will be augmented with the new values.

application.yml. 

spring:
   cloud:
     gateway:
@@ -155,7 +155,7 @@ their default values:

    uri: https://example.org filters: - MapRequestHeader=Bar, X-Request-Foo

-

This will add X-Request-Foo:<values> header to the downstream request’s with updated values from the incoming http request Bar header.

5.8 PrefixPath GatewayFilter Factory

The PrefixPath GatewayFilter Factory takes a single prefix parameter.

application.yml.  +

This will add X-Request-Foo:<values> header to the downstream request’s with updated values from the incoming http request Bar header.

6.8 PrefixPath GatewayFilter Factory

The PrefixPath GatewayFilter Factory takes a single prefix parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -164,7 +164,7 @@ their default values:

    uri: https://example.org filters: - PrefixPath=/mypath

-

This will prefix /mypath to the path of all matching requests. So a request to /hello, would be sent to /mypath/hello.

5.9 PreserveHostHeader GatewayFilter Factory

The PreserveHostHeader GatewayFilter Factory has not parameters. This filter, sets a request attribute that the routing filter will inspect to determine if the original host header should be sent, rather than the host header determined by the http client.

application.yml.  +

This will prefix /mypath to the path of all matching requests. So a request to /hello, would be sent to /mypath/hello.

6.9 PreserveHostHeader GatewayFilter Factory

The PreserveHostHeader GatewayFilter Factory has no parameters. This filter, sets a request attribute that the routing filter will inspect to determine if the original host header should be sent, rather than the host header determined by the http client.

application.yml. 

spring:
   cloud:
     gateway:
@@ -173,14 +173,14 @@ their default values:

    uri: https://example.org filters: - PreserveHostHeader

-

5.10 RequestRateLimiter GatewayFilter Factory

The RequestRateLimiter GatewayFilter Factory is uses a RateLimiter implementation to determine if the current request is allowed to proceed. If it is not, a status of HTTP 429 - Too Many Requests (by default) is returned.

This filter takes an optional keyResolver parameter and parameters specific to the rate limiter (see below).

keyResolver is a bean that implements the KeyResolver interface. In configuration, reference the bean by name using SpEL. #{@myKeyResolver} is a SpEL expression referencing a bean with the name myKeyResolver.

KeyResolver.java.  +

6.10 RequestRateLimiter GatewayFilter Factory

The RequestRateLimiter GatewayFilter Factory is uses a RateLimiter implementation to determine if the current request is allowed to proceed. If it is not, a status of HTTP 429 - Too Many Requests (by default) is returned.

This filter takes an optional keyResolver parameter and parameters specific to the rate limiter (see below).

keyResolver is a bean that implements the KeyResolver interface. In configuration, reference the bean by name using SpEL. #{@myKeyResolver} is a SpEL expression referencing a bean with the name myKeyResolver.

KeyResolver.java. 

public interface KeyResolver {
 	Mono<String> resolve(ServerWebExchange exchange);
 }

The KeyResolver interface allows pluggable strategies to derive the key for limiting requests. In future milestones, there will be some KeyResolver implementations.

The default implementation of KeyResolver is the PrincipalNameKeyResolver which retrieves the Principal from the ServerWebExchange and calls Principal.getName().

By default, if the KeyResolver does not find a key, requests will be denied. This behavior can be adjust with the spring.cloud.gateway.filter.request-rate-limiter.deny-empty-key (true or false) and spring.cloud.gateway.filter.request-rate-limiter.empty-key-status-code properties.

[Note]Note

The RequestRateLimiter is not configurable via the "shortcut" notation. The example below is invalid

application.properties. 

# INVALID SHORTCUT CONFIGURATION
 spring.cloud.gateway.routes[0].filters[0]=RequestRateLimiter=2, 2, #{@userkeyresolver}

-

5.10.1 Redis RateLimiter

The redis implementation is based off of work done at Stripe. It requires the use of the spring-boot-starter-data-redis-reactive Spring Boot starter.

The algorithm used is the Token Bucket Algorithm.

The redis-rate-limiter.replenishRate is how many requests per second do you want a user to be allowed to do, without any dropped requests. This is the rate that the token bucket is filled.

The redis-rate-limiter.burstCapacity is the maximum number of requests a user is allowed to do in a single second. This is the number of tokens the token bucket can hold. Setting this value to zero will block all requests.

A steady rate is accomplished by setting the same value in replenishRate and burstCapacity. Temporary bursts can be allowed by setting burstCapacity higher than replenishRate. In this case, the rate limiter needs to be allowed some time between bursts (according to replenishRate), as 2 consecutive bursts will result in dropped requests (HTTP 429 - Too Many Requests).

application.yml.  +

6.10.1 Redis RateLimiter

The redis implementation is based off of work done at Stripe. It requires the use of the spring-boot-starter-data-redis-reactive Spring Boot starter.

The algorithm used is the Token Bucket Algorithm.

The redis-rate-limiter.replenishRate is how many requests per second do you want a user to be allowed to do, without any dropped requests. This is the rate that the token bucket is filled.

The redis-rate-limiter.burstCapacity is the maximum number of requests a user is allowed to do in a single second. This is the number of tokens the token bucket can hold. Setting this value to zero will block all requests.

A steady rate is accomplished by setting the same value in replenishRate and burstCapacity. Temporary bursts can be allowed by setting burstCapacity higher than replenishRate. In this case, the rate limiter needs to be allowed some time between bursts (according to replenishRate), as 2 consecutive bursts will result in dropped requests (HTTP 429 - Too Many Requests).

application.yml. 

spring:
   cloud:
     gateway:
@@ -209,7 +209,7 @@ KeyResolver userKeyResolver() {
           args:
             rate-limiter: "#{@myRateLimiter}"
             key-resolver: "#{@userKeyResolver}"

-

5.11 RedirectTo GatewayFilter Factory

The RedirectTo GatewayFilter Factory takes a status and a url parameter. The status should be a 300 series redirect http code, such as 301. The url should be a valid url. This will be the value of the Location header.

application.yml.  +

6.11 RedirectTo GatewayFilter Factory

The RedirectTo GatewayFilter Factory takes a status and a url parameter. The status should be a 300 series redirect http code, such as 301. The url should be a valid url. This will be the value of the Location header.

application.yml. 

spring:
   cloud:
     gateway:
@@ -218,7 +218,7 @@ KeyResolver userKeyResolver() {
         uri: https://example.org
         filters:
         - RedirectTo=302, https://acme.org

-

This will send a status 302 with a Location:https://acme.org header to perform a redirect.

5.12 RemoveHopByHopHeadersFilter GatewayFilter Factory

The RemoveHopByHopHeadersFilter GatewayFilter Factory removes headers from forwarded requests. The default list of headers that is removed comes from the IETF.

The default removed headers are:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailer
  • Transfer-Encoding
  • Upgrade

To change this, set the spring.cloud.gateway.filter.remove-non-proxy-headers.headers property to the list of header names to remove.

5.13 RemoveRequestHeader GatewayFilter Factory

The RemoveRequestHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml.  +

This will send a status 302 with a Location:https://acme.org header to perform a redirect.

6.12 RemoveRequestHeader GatewayFilter Factory

The RemoveRequestHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml. 

spring:
   cloud:
     gateway:
@@ -227,7 +227,7 @@ KeyResolver userKeyResolver() {
         uri: https://example.org
         filters:
         - RemoveRequestHeader=X-Request-Foo

-

This will remove the X-Request-Foo header before it is sent downstream.

5.14 RemoveResponseHeader GatewayFilter Factory

The RemoveResponseHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml.  +

This will remove the X-Request-Foo header before it is sent downstream.

6.13 RemoveResponseHeader GatewayFilter Factory

The RemoveResponseHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml. 

spring:
   cloud:
     gateway:
@@ -238,7 +238,7 @@ KeyResolver userKeyResolver() {
         - RemoveResponseHeader=X-Response-Foo

This will remove the X-Response-Foo header from the response before it is returned to the gateway client.

To remove any kind of sensitive header you should configure this filter for any routes that you may want to do so. In addition you can configure this filter once using spring.cloud.gateway.default-filters -and have it applied to all routes.

5.15 RewritePath GatewayFilter Factory

The RewritePath GatewayFilter Factory takes a path regexp parameter and a replacement parameter. This uses Java regular expressions for a flexible way to rewrite the request path.

application.yml.  +and have it applied to all routes.

6.14 RewritePath GatewayFilter Factory

The RewritePath GatewayFilter Factory takes a path regexp parameter and a replacement parameter. This uses Java regular expressions for a flexible way to rewrite the request path.

application.yml. 

spring:
   cloud:
     gateway:
@@ -249,7 +249,7 @@ and have it applied to all routes.

- Path=/foo/** filters: - RewritePath=/foo(?<segment>/?.*), $\{segment}

-

For a request path of /foo/bar, this will set the path to /bar before making the downstream request. Notice the $\ which is replaced with $ because of the YAML spec.

5.16 RewriteLocationResponseHeader GatewayFilter Factory

The RewriteLocationResponseHeader GatewayFilter Factory modifies the value of Location response header, usually to get rid of backend specific details. It takes stripVersionMode, locationHeaderName, hostValue, and protocolsRegex parameters.

application.yml.  +

For a request path of /foo/bar, this will set the path to /bar before making the downstream request. Notice the $\ which is replaced with $ because of the YAML spec.

6.15 RewriteLocationResponseHeader GatewayFilter Factory

The RewriteLocationResponseHeader GatewayFilter Factory modifies the value of Location response header, usually to get rid of backend specific details. It takes stripVersionMode, locationHeaderName, hostValue, and protocolsRegex parameters.

application.yml. 

spring:
   cloud:
     gateway:
@@ -258,7 +258,7 @@ and have it applied to all routes.

uri: http://example.org filters: - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,

-

For example, for a request POST https://api.example.com/some/object/name, Location response header value https://object-service.prod.example.net/v2/some/object/id will be rewritten as https://api.example.com/some/object/id.

Parameter stripVersionMode has the following possible values: NEVER_STRIP, AS_IN_REQUEST (default), ALWAYS_STRIP.

  • NEVER_STRIP - Version will not be stripped, even if the original request path contains no version
  • AS_IN_REQUEST - Version will be stripped only if the original request path contains no version
  • ALWAYS_STRIP - Version will be stripped, even if the original request path contains version

Parameter hostValue, if provided, will be used to replace the host:port portion of the response Location header. If not provided, the value of the Host request header will be used.

Parameter protocolsRegex must be a valid regex String, against which the protocol name will be matched. If not matched, the filter will do nothing. Default is http|https|ftp|ftps.

5.17 RewriteResponseHeader GatewayFilter Factory

The RewriteResponseHeader GatewayFilter Factory takes name, regexp, and replacement parameters. It uses Java regular expressions for a flexible way to rewrite the response header value.

application.yml.  +

For example, for a request POST https://api.example.com/some/object/name, Location response header value https://object-service.prod.example.net/v2/some/object/id will be rewritten as https://api.example.com/some/object/id.

Parameter stripVersionMode has the following possible values: NEVER_STRIP, AS_IN_REQUEST (default), ALWAYS_STRIP.

  • NEVER_STRIP - Version will not be stripped, even if the original request path contains no version
  • AS_IN_REQUEST - Version will be stripped only if the original request path contains no version
  • ALWAYS_STRIP - Version will be stripped, even if the original request path contains version

Parameter hostValue, if provided, will be used to replace the host:port portion of the response Location header. If not provided, the value of the Host request header will be used.

Parameter protocolsRegex must be a valid regex String, against which the protocol name will be matched. If not matched, the filter will do nothing. Default is http|https|ftp|ftps.

6.16 RewriteResponseHeader GatewayFilter Factory

The RewriteResponseHeader GatewayFilter Factory takes name, regexp, and replacement parameters. It uses Java regular expressions for a flexible way to rewrite the response header value.

application.yml. 

spring:
   cloud:
     gateway:
@@ -267,7 +267,7 @@ and have it applied to all routes.

uri: https://example.org filters: - RewriteResponseHeader=X-Response-Foo, , password=[^&]+, password=***

-

For a header value of /42?user=ford&password=omg!what&flag=true, it will be set to /42?user=ford&password=***&flag=true after making the downstream request. Please use $\ to mean $ because of the YAML spec.

5.18 SaveSession GatewayFilter Factory

The SaveSession GatewayFilter Factory forces a WebSession::save operation before forwarding the call downstream. This is of particular use when +

For a header value of /42?user=ford&password=omg!what&flag=true, it will be set to /42?user=ford&password=***&flag=true after making the downstream request. Please use $\ to mean $ because of the YAML spec.

6.17 SaveSession GatewayFilter Factory

The SaveSession GatewayFilter Factory forces a WebSession::save operation before forwarding the call downstream. This is of particular use when using something like Spring Session with a lazy data store and need to ensure the session state has been saved before making the forwarded call.

application.yml. 

spring:
   cloud:
@@ -279,7 +279,7 @@ using something like         - Path=/foo/**
         filters:
         - SaveSession

-

If you are integrating Spring Security with Spring Session, and want to ensure security details have been forwarded to the remote process, this is critical.

5.19 SecureHeaders GatewayFilter Factory

The SecureHeaders GatewayFilter Factory adds a number of headers to the response at the recommendation from this blog post.

The following headers are added (along with default values):

  • X-Xss-Protection:1; mode=block
  • Strict-Transport-Security:max-age=631138519
  • X-Frame-Options:DENY
  • X-Content-Type-Options:nosniff
  • Referrer-Policy:no-referrer
  • Content-Security-Policy:default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'
  • X-Download-Options:noopen
  • X-Permitted-Cross-Domain-Policies:none

To change the default values set the appropriate property in the spring.cloud.gateway.filter.secure-headers namespace:

Property to change:

  • xss-protection-header
  • strict-transport-security
  • frame-options
  • content-type-options
  • referrer-policy
  • content-security-policy
  • download-options
  • permitted-cross-domain-policies

To disable the default values set the property spring.cloud.gateway.filter.secure-headers.disable with comma separated values.

[Note]Note

Need use lowercase and full name of secure headers.

The following values can use:

  • x-xss-protection
  • strict-transport-security
  • x-frame-options
  • x-content-type-options
  • referrer-policy
  • content-security-policy
  • x-download-options
  • x-permitted-cross-domain-policies

Example: spring.cloud.gateway.filter.secure-headers.disable=x-frame-options,strict-transport-security

5.20 SetPath GatewayFilter Factory

The SetPath GatewayFilter Factory takes a path template parameter. It offers a simple way to manipulate the request path by allowing templated segments of the path. This uses the uri templates from Spring Framework. Multiple matching segments are allowed.

application.yml.  +

If you are integrating Spring Security with Spring Session, and want to ensure security details have been forwarded to the remote process, this is critical.

6.18 SecureHeaders GatewayFilter Factory

The SecureHeaders GatewayFilter Factory adds a number of headers to the response at the recommendation from this blog post.

The following headers are added (along with default values):

  • X-Xss-Protection:1; mode=block
  • Strict-Transport-Security:max-age=631138519
  • X-Frame-Options:DENY
  • X-Content-Type-Options:nosniff
  • Referrer-Policy:no-referrer
  • Content-Security-Policy:default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'
  • X-Download-Options:noopen
  • X-Permitted-Cross-Domain-Policies:none

To change the default values set the appropriate property in the spring.cloud.gateway.filter.secure-headers namespace:

Property to change:

  • xss-protection-header
  • strict-transport-security
  • frame-options
  • content-type-options
  • referrer-policy
  • content-security-policy
  • download-options
  • permitted-cross-domain-policies

To disable the default values set the property spring.cloud.gateway.filter.secure-headers.disable with comma separated values.

[Note]Note

Need use lowercase and full name of secure headers.

The following values can use:

  • x-xss-protection
  • strict-transport-security
  • x-frame-options
  • x-content-type-options
  • referrer-policy
  • content-security-policy
  • x-download-options
  • x-permitted-cross-domain-policies

Example: spring.cloud.gateway.filter.secure-headers.disable=x-frame-options,strict-transport-security

6.19 SetPath GatewayFilter Factory

The SetPath GatewayFilter Factory takes a path template parameter. It offers a simple way to manipulate the request path by allowing templated segments of the path. This uses the uri templates from Spring Framework. Multiple matching segments are allowed.

application.yml. 

spring:
   cloud:
     gateway:
@@ -290,7 +290,7 @@ using something like         - Path=/foo/{segment}
         filters:
         - SetPath=/{segment}

-

For a request path of /foo/bar, this will set the path to /bar before making the downstream request.

5.21 SetRequestHeader GatewayFilter Factory

The SetRequestHeader GatewayFilter Factory takes name and value parameters.

application.yml.  +

For a request path of /foo/bar, this will set the path to /bar before making the downstream request.

6.20 SetRequestHeader GatewayFilter Factory

The SetRequestHeader GatewayFilter Factory takes name and value parameters.

application.yml. 

spring:
   cloud:
     gateway:
@@ -310,7 +310,7 @@ using something like         - Host: {segment}.myhost.org
         filters:
         - SetRequestHeader=foo, bar-{segment}

-

5.22 SetResponseHeader GatewayFilter Factory

The SetResponseHeader GatewayFilter Factory takes name and value parameters.

application.yml.  +

6.21 SetResponseHeader GatewayFilter Factory

The SetResponseHeader GatewayFilter Factory takes name and value parameters.

application.yml. 

spring:
   cloud:
     gateway:
@@ -330,7 +330,7 @@ using something like         - Host: {segment}.myhost.org
         filters:
         - SetResponseHeader=foo, bar-{segment}

-

5.23 SetStatus GatewayFilter Factory

The SetStatus GatewayFilter Factory takes a single status parameter. It must be a valid Spring HttpStatus. It may be the integer value 404 or the string representation of the enumeration NOT_FOUND.

application.yml.  +

6.22 SetStatus GatewayFilter Factory

The SetStatus GatewayFilter Factory takes a single status parameter. It must be a valid Spring HttpStatus. It may be the integer value 404 or the string representation of the enumeration NOT_FOUND.

application.yml. 

spring:
   cloud:
     gateway:
@@ -343,7 +343,7 @@ using something like         uri: https://example.org
         filters:
         - SetStatus=401

-

In either case, the HTTP status of the response will be set to 401.

5.24 StripPrefix GatewayFilter Factory

The StripPrefix GatewayFilter Factory takes one paramter, parts. The parts parameter indicated the number of parts in the path to strip from the request before sending it downstream.

application.yml.  +

In either case, the HTTP status of the response will be set to 401.

6.23 StripPrefix GatewayFilter Factory

The StripPrefix GatewayFilter Factory takes one paramter, parts. The parts parameter indicated the number of parts in the path to strip from the request before sending it downstream.

application.yml. 

spring:
   cloud:
     gateway:
@@ -354,7 +354,7 @@ using something like         - Path=/name/**
         filters:
         - StripPrefix=2

-

When a request is made through the gateway to /name/bar/foo the request made to nameservice will look like http://nameservice/foo.

5.25 Retry GatewayFilter Factory

The Retry GatewayFilter Factory support following set of parameters:

  • retries: the number of retries that should be attempted
  • statuses: the HTTP status codes that should be retried, represented using org.springframework.http.HttpStatus
  • methods: the HTTP methods that should be retried, represented using org.springframework.http.HttpMethod
  • series: the series of status codes to be retried, represented using org.springframework.http.HttpStatus.Series
  • exceptions: list of exceptions thrown that should be retried
  • backoff: configured exponential backoff for the retries. Retries are performed after a backoff interval of firstBackoff * (factor ^ n) where n is the iteration. +

    When a request is made through the gateway to /name/bar/foo the request made to nameservice will look like http://nameservice/foo.

6.24 Retry GatewayFilter Factory

The Retry GatewayFilter Factory support following set of parameters:

  • retries: the number of retries that should be attempted
  • statuses: the HTTP status codes that should be retried, represented using org.springframework.http.HttpStatus
  • methods: the HTTP methods that should be retried, represented using org.springframework.http.HttpMethod
  • series: the series of status codes to be retried, represented using org.springframework.http.HttpStatus.Series
  • exceptions: list of exceptions thrown that should be retried
  • backoff: configured exponential backoff for the retries. Retries are performed after a backoff interval of firstBackoff * (factor ^ n) where n is the iteration. If maxBackoff is configured, the maximum backoff applied will be limited to maxBackoff. If basedOnPreviousValue is true, backoff will be calculated using prevBackoff * factor.

The following defaults are configured for Retry filter if enabled:

  • retries — 3 times
  • series — 5XX series
  • methods — GET method
  • exceptions — IOException and TimeoutException
  • backoff — disabled

application.yml. 

spring:
@@ -370,12 +370,13 @@ If basedOnPreviousValue is true, backoff will be ca
           args:
             retries: 3
             statuses: BAD_GATEWAY
+            methods: GET,POST
             backoff:
               firstBackoff: 10ms
               maxBackoff: 50ms
               factor: 2
               basedOnPreviousValue: false

-

[Note]Note

The retry filter does not currently support retrying with a body (e.g. for POST or PUT requests with a body).

[Note]Note

When using the retry filter with a forward: prefixed URL, the target endpoint should be written carefully so that in case of an error it does not do anything that could result in a response being sent to the client and committed. For example, if the target endpoint is an annotated controller, the target controller method should not return ResponseEntity with an error status code. Instead it should throw an Exception, or signal an error, e.g. via a Mono.error(ex) return value, which the retry filter can be configured to handle by retrying.

5.26 RequestSize GatewayFilter Factory

The RequestSize GatewayFilter Factory can restrict a request from reaching the downstream service , when the request size is greater than the permissible limit. The filter takes RequestSize as parameter which is the permissible size limit of the request defined in bytes.

application.yml.  +

[Note]Note

When using the retry filter with a forward: prefixed URL, the target endpoint should be written carefully so that in case of an error it does not do anything that could result in a response being sent to the client and committed. For example, if the target endpoint is an annotated controller, the target controller method should not return ResponseEntity with an error status code. Instead it should throw an Exception, or signal an error, e.g. via a Mono.error(ex) return value, which the retry filter can be configured to handle by retrying.

[Warning]Warning

When using the retry filter with any HTTP method with a body, the body will be cached and the gateway will become memory constrained. The body is cached in a request attribute defined by ServerWebExchangeUtils.CACHED_REQUEST_BODY_ATTR. The type of the object is a org.springframework.core.io.buffer.DataBuffer.

6.25 RequestSize GatewayFilter Factory

The RequestSize GatewayFilter Factory can restrict a request from reaching the downstream service , when the request size is greater than the permissible limit. The filter takes a maxSize parameter which is the permissible size limit of the request. The maxSize is a `DataSize type, so values can be defined as a number followed by an optional DataUnit suffix such as 'KB' or 'MB'. The default is 'B' for bytes.

application.yml. 

spring:
   cloud:
     gateway:
@@ -388,7 +389,7 @@ If basedOnPreviousValue is true, backoff will be ca
       - name: RequestSize
         args:
           maxSize: 5000000

-

The RequestSize GatewayFilter Factory set the response status as 413 Payload Too Large with a additional header errorMessage when the Request is rejected due to size. Following is an example of such an errorMessage .

errorMessage : Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB

[Note]Note

The default Request size will be set to 5 MB if not provided as filter argument in route definition.

5.27 Modify Request Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

This filter can be used to modify the request body before it is sent downstream by the Gateway.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
+

The RequestSize GatewayFilter Factory set the response status as 413 Payload Too Large with a additional header errorMessage when the Request is rejected due to size. Following is an example of such an errorMessage .

errorMessage : Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB

[Note]Note

The default Request size will be set to 5 MB if not provided as filter argument in route definition.

6.26 Modify Request Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

The ModifyRequestBody filter can be used to modify the request body before it is sent downstream by the Gateway.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
 public RouteLocator routes(RouteLocatorBuilder builder) {
     return builder.routes()
         .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
@@ -414,7 +415,7 @@ If basedOnPreviousValue is true, backoff will be ca
     public void setMessage(String message) {
         this.message = message;
     }
-}

5.28 Modify Response Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

This filter can be used to modify the response body before it is sent back to the Client.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
+}

6.27 Modify Response Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

The ModifyResponseBody filter can be used to modify the response body before it is sent back to the Client.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
 public RouteLocator routes(RouteLocatorBuilder builder) {
     return builder.routes()
         .route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
@@ -422,7 +423,7 @@ If basedOnPreviousValue is true, backoff will be ca
         		.modifyResponseBody(String.class, String.class,
         		    (exchange, s) -> Mono.just(s.toUpperCase()))).uri(uri)
         .build();
-}

5.29 Default Filters

If you would like to add a filter and apply it to all routes you can use spring.cloud.gateway.default-filters. +}

6.28 Default Filters

If you would like to add a filter and apply it to all routes you can use spring.cloud.gateway.default-filters. This property takes a list of filters

application.yml. 

spring:
   cloud:
@@ -430,4 +431,4 @@ This property takes a list of filters

application.yml.  default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar - PrefixPath=/httpbin

-

\ No newline at end of file +

\ No newline at end of file diff --git a/2.1.x/multi/multi__global_filters.html b/2.1.x/multi/multi__global_filters.html index 8a266d0e..750ffcd0 100644 --- a/2.1.x/multi/multi__global_filters.html +++ b/2.1.x/multi/multi__global_filters.html @@ -1,6 +1,6 @@ - 6. Global Filters

6. Global Filters

The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).

6.1 Combined Global Filter and GatewayFilter Ordering

When a request comes in (and matches a Route) the Filtering Web Handler will add all instances of GlobalFilter and all route specific instances of GatewayFilter to a filter chain. This combined filter chain is sorted by the org.springframework.core.Ordered interface, which can be set by implementing the getOrder() method.

As Spring Cloud Gateway distinguishes between "pre" and "post" phases for filter logic execution (see: How it Works), the filter with the highest precedence will be the first in the "pre"-phase and the last in the "post"-phase.

ExampleConfiguration.java.  + 7. Global Filters

7. Global Filters

The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).

7.1 Combined Global Filter and GatewayFilter Ordering

When a request comes in (and matches a Route) the Filtering Web Handler will add all instances of GlobalFilter and all route specific instances of GatewayFilter to a filter chain. This combined filter chain is sorted by the org.springframework.core.Ordered interface, which can be set by implementing the getOrder() method.

As Spring Cloud Gateway distinguishes between "pre" and "post" phases for filter logic execution (see: How it Works), the filter with the highest precedence will be the first in the "pre"-phase and the last in the "post"-phase.

ExampleConfiguration.java. 

@Bean
 public GlobalFilter customFilter() {
     return new CustomGlobalFilter();
@@ -19,7 +19,7 @@
         return -1;
     }
 }

-

6.2 Forward Routing Filter

The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.

6.3 LoadBalancerClient Filter

The LoadBalancerClientFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud LoadBalancerClient to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. The filter will also look in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals lb and then the same rules apply.

application.yml.  +

7.2 Forward Routing Filter

The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.

7.3 LoadBalancerClient Filter

The LoadBalancerClientFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud LoadBalancerClient to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. The filter will also look in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals lb and then the same rules apply.

application.yml. 

spring:
   cloud:
     gateway:
@@ -35,9 +35,9 @@ but the ServiceInstance indicates it is not secure,
 HTTP.  The opposite situation can also apply.  However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the
 route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the
 route URL will override the ServiceInstance configuration.

[Warning]Warning

LoadBalancerClientFilter uses a blocking Ribbon LoadBalancerClient under the hood. -We suggest you use ReactiveLoadBalancerClientFilter instead. +We suggest you use ReactiveLoadBalancerClientFilter instead. You can switch to using it by adding org.springframework.cloud:spring-cloud-loadbalancer dependency to your project -and setting the value of the spring.cloud.loadbalancer.ribbon.enabled to false.

6.4 ReactiveLoadBalancerClientFilter

The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute +and setting the value of the spring.cloud.loadbalancer.ribbon.enabled to false.

7.4 ReactiveLoadBalancerClientFilter

The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud ReactorLoadBalancer to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute. The unmodified @@ -58,7 +58,7 @@ the scheme specified in the request made to the Gateway. For example, if the re but the ServiceInstance indicates it is not secure, then the downstream request will be made over HTTP. The opposite situation can also apply. However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the -route URL will override the ServiceInstance configuration.

6.5 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

6.6 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

6.7 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

6.8 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml.  +route URL will override the ServiceInstance configuration.

7.5 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

7.6 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

7.7 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

7.8 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml. 

spring:
   cloud:
     gateway:
@@ -73,7 +73,7 @@ route URL will override the ServiceInstance configu
         uri: ws://localhost:3001
         predicates:
         - Path=/websocket/**

-

6.9 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

  • routeId: The route id
  • routeUri: The URI that the API will be routed to
  • outcome: Outcome as classified by HttpStatus.Series
  • status: Http Status of the request returned to the client
  • httpStatusCode: Http Status of the request returned to the client
  • httpMethod: The Http method used for the request

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

6.10 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted +

7.9 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

  • routeId: The route id
  • routeUri: The URI that the API will be routed to
  • outcome: Outcome as classified by HttpStatus.Series
  • status: Http Status of the request returned to the client
  • httpStatusCode: Http Status of the request returned to the client
  • httpMethod: The Http method used for the request

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

7.10 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted to the exchange attributes. Once a request has been marked as routed, other routing filters will not route the request again, essentially skipping the filter. There are convenience methods that you can use to mark an exchange as routed -or check if an exchange has already been routed.

  • ServerWebExchangeUtils.isAlreadyRouted takes a ServerWebExchange object and checks if it has been "routed"
  • ServerWebExchangeUtils.setAlreadyRouted takes a ServerWebExchange object and marks it as "routed"
\ No newline at end of file +or check if an exchange has already been routed.

  • ServerWebExchangeUtils.isAlreadyRouted takes a ServerWebExchange object and checks if it has been "routed"
  • ServerWebExchangeUtils.setAlreadyRouted takes a ServerWebExchange object and marks it as "routed"
\ No newline at end of file diff --git a/2.1.x/multi/multi__httpheadersfilters.html b/2.1.x/multi/multi__httpheadersfilters.html new file mode 100644 index 00000000..fd5e9987 --- /dev/null +++ b/2.1.x/multi/multi__httpheadersfilters.html @@ -0,0 +1,3 @@ + + + 8. HttpHeadersFilters

8. HttpHeadersFilters

HttpHeadersFilters are applied to requests before sending them downstream, such as in the NettyRoutingFilter.

8.1 Forwarded Headers Filter

The Forwarded Headers Filter creates a Forwarded header to send to the downstream service. It adds the Host header, scheme and port of the current request to any existing Forwarded header.

8.2 RemoveHopByHop Headers Filter

The RemoveHopByHop Headers Filter removes headers from forwarded requests. The default list of headers that is removed comes from the IETF.

The default removed headers are:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailer
  • Transfer-Encoding
  • Upgrade

To change this, set the spring.cloud.gateway.filter.remove-non-proxy-headers.headers property to the list of header names to remove.

8.3 XForwarded Headers Filter

The XForwarded Headers Filter creates various a X-Forwarded-* headers to send to the downstream service. It users the Host header, scheme, port and path of the current request to create the various headers.

Creating of individual headers can be controlled by the following boolean properties (defaults to true):

  • spring.cloud.gateway.x-forwarded.for.enabled
  • spring.cloud.gateway.x-forwarded.host.enabled
  • spring.cloud.gateway.x-forwarded.port.enabled
  • spring.cloud.gateway.x-forwarded.proto.enabled
  • spring.cloud.gateway.x-forwarded.prefix.enabled

Appending multiple headers can be controlled by the following boolean properties (defaults to true):

  • spring.cloud.gateway.x-forwarded.for.append
  • spring.cloud.gateway.x-forwarded.host.append
  • spring.cloud.gateway.x-forwarded.port.append
  • spring.cloud.gateway.x-forwarded.proto.append
  • spring.cloud.gateway.x-forwarded.prefix.append
\ No newline at end of file diff --git a/2.1.x/multi/multi__reactor_netty_access_logs.html b/2.1.x/multi/multi__reactor_netty_access_logs.html index 5c5352da..ba0776e8 100644 --- a/2.1.x/multi/multi__reactor_netty_access_logs.html +++ b/2.1.x/multi/multi__reactor_netty_access_logs.html @@ -1,6 +1,6 @@ - 9. Reactor Netty Access Logs

9. Reactor Netty Access Logs

To enable Reactor Netty access logs, set -Dreactor.netty.http.server.accessLogEnabled=true. (It must be a Java System Property, not a Spring Boot property).

The logging system can be configured to have a separate access log file. Below is an example logback configuration:

logback.xml.  + 11. Reactor Netty Access Logs

11. Reactor Netty Access Logs

To enable Reactor Netty access logs, set -Dreactor.netty.http.server.accessLogEnabled=true. (It must be a Java System Property, not a Spring Boot property).

The logging system can be configured to have a separate access log file. Below is an example logback configuration:

logback.xml. 

    <appender name="accessLog" class="ch.qos.logback.core.FileAppender">
         <file>access_log.log</file>
         <encoder>
@@ -14,4 +14,4 @@
     <logger name="reactor.netty.http.server.AccessLog" level="INFO" additivity="false">
         <appender-ref ref="async"/>
     </logger>

-

\ No newline at end of file +

\ No newline at end of file diff --git a/2.1.x/multi/multi__tls_ssl.html b/2.1.x/multi/multi__tls_ssl.html index 60247b6b..8fa0e851 100644 --- a/2.1.x/multi/multi__tls_ssl.html +++ b/2.1.x/multi/multi__tls_ssl.html @@ -1,6 +1,6 @@ - 7. TLS / SSL

7. TLS / SSL

The Gateway can listen for requests on https by following the usual Spring server configuration. Example:

application.yml.  + 9. TLS / SSL

9. TLS / SSL

The Gateway can listen for requests on https by following the usual Spring server configuration. Example:

application.yml. 

server:
   ssl:
     enabled: true
@@ -24,7 +24,7 @@
           trustedX509Certificates:
           - cert1.pem
           - cert2.pem

-

If the Spring Cloud Gateway is not provisioned with trusted certificates the default trust store is used (which can be overriden with system property javax.net.ssl.trustStore).

7.1 TLS Handshake

The Gateway maintains a client pool that it uses to route to backends. When communicating over https the client initiates a TLS handshake. A number of timeouts are assoicated with this handshake. These timeouts can be configured (defaults shown):

application.yml.  +

If the Spring Cloud Gateway is not provisioned with trusted certificates the default trust store is used (which can be overriden with system property javax.net.ssl.trustStore).

9.1 TLS Handshake

The Gateway maintains a client pool that it uses to route to backends. When communicating over https the client initiates a TLS handshake. A number of timeouts are assoicated with this handshake. These timeouts can be configured (defaults shown):

application.yml. 

spring:
   cloud:
     gateway:
@@ -33,4 +33,4 @@
           handshake-timeout-millis: 10000
           close-notify-flush-timeout-millis: 3000
           close-notify-read-timeout-millis: 0

-

\ No newline at end of file +

\ No newline at end of file diff --git a/2.1.x/multi/multi_gateway-how-it-works.html b/2.1.x/multi/multi_gateway-how-it-works.html index 36355098..68d12705 100644 --- a/2.1.x/multi/multi_gateway-how-it-works.html +++ b/2.1.x/multi/multi_gateway-how-it-works.html @@ -1,3 +1,3 @@ - 3. How It Works

3. How It Works

Spring Cloud Gateway Diagram

Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler. This handler runs sends the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line, is that filters may execute logic before the proxy request is sent or after. All "pre" filter logic is executed, then the proxy request is made. After the proxy request is made, the "post" filter logic is executed.

[Note]Note

URIs defined in routes without a port will get a default port set to 80 and 443 for HTTP and HTTPS URIs respectively.

\ No newline at end of file + 3. How It Works

3. How It Works

Spring Cloud Gateway Diagram

Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler. This handler runs sends the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line, is that filters may execute logic before the proxy request is sent or after. All "pre" filter logic is executed, then the proxy request is made. After the proxy request is made, the "post" filter logic is executed.

[Note]Note

URIs defined in routes without a port will get a default port set to 80 and 443 for HTTP and HTTPS URIs respectively.

\ No newline at end of file diff --git a/2.1.x/multi/multi_gateway-request-predicates-factories.html b/2.1.x/multi/multi_gateway-request-predicates-factories.html index fb66b8d8..920b3ec0 100644 --- a/2.1.x/multi/multi_gateway-request-predicates-factories.html +++ b/2.1.x/multi/multi_gateway-request-predicates-factories.html @@ -1,6 +1,6 @@ - 4. Route Predicate Factories

4. Route Predicate Factories

Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping infrastructure. Spring Cloud Gateway includes many built-in Route Predicate Factories. All of these predicates match on different attributes of the HTTP request. Multiple Route Predicate Factories can be combined and are combined via logical and.

4.1 After Route Predicate Factory

The After Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen after the current datetime.

application.yml.  + 5. Route Predicate Factories

5. Route Predicate Factories

Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping infrastructure. Spring Cloud Gateway includes many built-in Route Predicate Factories. All of these predicates match on different attributes of the HTTP request. Multiple Route Predicate Factories can be combined and are combined via logical and.

5.1 After Route Predicate Factory

The After Route Predicate Factory takes one parameter, a datetime (which is a java ZonedDateTime). This predicate matches requests that happen after the current datetime.

application.yml. 

spring:
   cloud:
     gateway:
@@ -9,7 +9,7 @@
         uri: https://example.org
         predicates:
         - After=2017-01-20T17:42:47.789-07:00[America/Denver]

-

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver).

4.2 Before Route Predicate Factory

The Before Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen before the current datetime.

application.yml.  +

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver).

5.2 Before Route Predicate Factory

The Before Route Predicate Factory takes one parameter, a datetime(which is a java ZonedDateTime). This predicate matches requests that happen before the current datetime.

application.yml. 

spring:
   cloud:
     gateway:
@@ -18,7 +18,7 @@
         uri: https://example.org
         predicates:
         - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

-

This route matches any request before Jan 20, 2017 17:42 Mountain Time (Denver).

4.3 Between Route Predicate Factory

The Between Route Predicate Factory takes two parameters, datetime1 and datetime2. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1.

application.yml.  +

This route matches any request before Jan 20, 2017 17:42 Mountain Time (Denver).

5.3 Between Route Predicate Factory

The Between Route Predicate Factory takes two parameters, datetime1 and datetime2 which are java ZonedDateTime objects. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1.

application.yml. 

spring:
   cloud:
     gateway:
@@ -27,7 +27,7 @@
         uri: https://example.org
         predicates:
         - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

-

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver) and before Jan 21, 2017 17:42 Mountain Time (Denver). This could be useful for maintenance windows.

4.4 Cookie Route Predicate Factory

The Cookie Route Predicate Factory takes two parameters, the cookie name and a regular expression. This predicate matches cookies that have the given name and the value matches the regular expression.

application.yml.  +

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver) and before Jan 21, 2017 17:42 Mountain Time (Denver). This could be useful for maintenance windows.

5.4 Cookie Route Predicate Factory

The Cookie Route Predicate Factory takes two parameters, the cookie name and a regexp (which is a Java regular expression). This predicate matches cookies that have the given name and the value matches the regular expression.

application.yml. 

spring:
   cloud:
     gateway:
@@ -36,7 +36,7 @@
         uri: https://example.org
         predicates:
         - Cookie=chocolate, ch.p

-

This route matches the request has a cookie named chocolate who’s value matches the ch.p regular expression.

4.5 Header Route Predicate Factory

The Header Route Predicate Factory takes two parameters, the header name and a regular expression. This predicate matches with a header that has the given name and the value matches the regular expression.

application.yml.  +

This route matches the request has a cookie named chocolate who’s value matches the ch.p regular expression.

5.5 Header Route Predicate Factory

The Header Route Predicate Factory takes two parameters, the header name and a regexp (which is a Java regular expression). This predicate matches with a header that has the given name and the value matches the regular expression.

application.yml. 

spring:
   cloud:
     gateway:
@@ -45,7 +45,7 @@
         uri: https://example.org
         predicates:
         - Header=X-Request-Id, \d+

-

This route matches if the request has a header named X-Request-Id whos value matches the \d+ regular expression (has a value of one or more digits).

4.6 Host Route Predicate Factory

The Host Route Predicate Factory takes one parameter: a list of host name patterns. The pattern is an Ant style pattern with . as the separator. This predicates matches the Host header that matches the pattern.

application.yml.  +

This route matches if the request has a header named X-Request-Id whos value matches the \d+ regular expression (has a value of one or more digits).

5.6 Host Route Predicate Factory

The Host Route Predicate Factory takes one parameter: a list of host name patterns. The pattern is an Ant style pattern with . as the separator. This predicates matches the Host header that matches the pattern.

application.yml. 

spring:
   cloud:
     gateway:
@@ -54,7 +54,7 @@
         uri: https://example.org
         predicates:
         - Host=**.somehost.org,**.anotherhost.org

-

URI template variables are supported as well, such as {sub}.myhost.org.

This route would match if the request has a Host header has the value www.somehost.org or beta.somehost.org or www.anotherhost.org.

This predicate extracts the URI template variables (like sub defined in the example above) as a map of names and values and places it in the ServerWebExchange.getAttributes() with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. Those values are then available for use by GatewayFilter Factories

4.7 Method Route Predicate Factory

The Method Route Predicate Factory takes one or more parameters: the HTTP methods to match.

application.yml.  +

URI template variables are supported as well, such as {sub}.myhost.org.

This route would match if the request has a Host header has the value www.somehost.org or beta.somehost.org or www.anotherhost.org.

This predicate extracts the URI template variables (like sub defined in the example above) as a map of names and values and places it in the ServerWebExchange.getAttributes() with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. Those values are then available for use by GatewayFilter Factories

5.7 Method Route Predicate Factory

The Method Route Predicate Factory takes a methods argument which is one or more HTTP methods to match.

application.yml. 

spring:
   cloud:
     gateway:
@@ -63,7 +63,7 @@
         uri: https://example.org
         predicates:
         - Method=GET,POST

-

This route would match if the request method was a GET or a POST.

4.8 Path Route Predicate Factory

The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher patterns and an optional flag to matchOptionalTrailingSeparator.

application.yml.  +

This route would match if the request method was a GET or a POST.

5.8 Path Route Predicate Factory

The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher patterns and an optional flag to matchOptionalTrailingSeparator.

application.yml. 

spring:
   cloud:
     gateway:
@@ -74,7 +74,7 @@
         - Path=/foo/{segment},/bar/{segment}

This route would match if the request path was, for example: /foo/1 or /foo/bar or /bar/baz.

This predicate extracts the URI template variables (like segment defined in the example above) as a map of names and values and places it in the ServerWebExchange.getAttributes() with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. Those values are then available for use by GatewayFilter Factories

A utility method is available to make access to these variables easier.

Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
 
-String segment = uriVariables.get("segment");

4.9 Query Route Predicate Factory

The Query Route Predicate Factory takes two parameters: a required param and an optional regexp.

application.yml.  +String segment = uriVariables.get("segment");

5.9 Query Route Predicate Factory

The Query Route Predicate Factory takes two parameters: a required param and an optional regexp (which is a Java regular expression).

application.yml. 

spring:
   cloud:
     gateway:
@@ -92,7 +92,7 @@ String segment = uriVariables.get(        uri: https://example.org
         predicates:
         - Query=foo, ba.

-

This route would match if the request contained a foo query parameter whose value matched the ba. regexp, so bar and baz would match.

4.10 RemoteAddr Route Predicate Factory

The RemoteAddr Route Predicate Factory takes a list (min size 1) of CIDR-notation (IPv4 or IPv6) strings, e.g. 192.168.0.1/16 (where 192.168.0.1 is an IP address and 16 is a subnet mask).

application.yml.  +

This route would match if the request contained a foo query parameter whose value matched the ba. regexp, so bar and baz would match.

5.10 RemoteAddr Route Predicate Factory

The RemoteAddr Route Predicate Factory takes a list (min size 1) of sources, which are CIDR-notation (IPv4 or IPv6) strings, e.g. 192.168.0.1/16 (where 192.168.0.1 is an IP address and 16 is a subnet mask).

application.yml. 

spring:
   cloud:
     gateway:
@@ -101,7 +101,20 @@ String segment = uriVariables.get(        uri: https://example.org
         predicates:
         - RemoteAddr=192.168.1.1/24

-

This route would match if the remote address of the request was, for example, 192.168.1.10.

4.10.1 Modifying the way remote addresses are resolved

By default the RemoteAddr Route Predicate Factory uses the remote address from the incoming request. +

This route would match if the remote address of the request was, for example, 192.168.1.10.

5.11 Weight Route Predicate Factory

The Weight Route Predicate Factory takes two arguments group and weight (an int). The weights are calculated per group.

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: weight_high
+        uri: https://weighthigh.org
+        predicates:
+        - Weight=group1, 8
+      - id: weight_low
+        uri: https://weightlow.org
+        predicates:
+        - Weight=group1, 2

+

This route would forward ~80% of traffic to https://weighthigh.org and ~20% of traffic to https://weighlow.org

5.11.1 Modifying the way remote addresses are resolved

By default the RemoteAddr Route Predicate Factory uses the remote address from the incoming request. This may not match the actual client IP address if Spring Cloud Gateway sits behind a proxy layer.

You can customize the way that the remote address is resolved by setting a custom RemoteAddressResolver. Spring Cloud Gateway comes with one non-default remote address resolver which is based off of the X-Forwarded-For header, XForwardedRemoteAddressResolver.

XForwardedRemoteAddressResolver has two static constructor methods which take different approaches to security:

XForwardedRemoteAddressResolver::trustAll returns a RemoteAddressResolver which always takes the first IP address found in the X-Forwarded-For header. This approach is vulnerable to spoofing, as a malicious client could set an initial value for the X-Forwarded-For which would be accepted by the resolver.

XForwardedRemoteAddressResolver::maxTrustedIndex takes an index which correlates to the number of trusted infrastructure running in front of Spring Cloud Gateway. @@ -117,4 +130,4 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i .route("proxied-route", r -> r.remoteAddr(resolver, "10.10.1.1", "10.10.1.1/24") .uri("https://downstream2") -)

\ No newline at end of file +)
\ No newline at end of file diff --git a/2.1.x/multi/multi_spring-cloud-gateway.html b/2.1.x/multi/multi_spring-cloud-gateway.html index d583795c..741afa89 100644 --- a/2.1.x/multi/multi_spring-cloud-gateway.html +++ b/2.1.x/multi/multi_spring-cloud-gateway.html @@ -1,3 +1,3 @@ - Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Route Predicate Factories
4.1. After Route Predicate Factory
4.2. Before Route Predicate Factory
4.3. Between Route Predicate Factory
4.4. Cookie Route Predicate Factory
4.5. Header Route Predicate Factory
4.6. Host Route Predicate Factory
4.7. Method Route Predicate Factory
4.8. Path Route Predicate Factory
4.9. Query Route Predicate Factory
4.10. RemoteAddr Route Predicate Factory
4.10.1. Modifying the way remote addresses are resolved
5. GatewayFilter Factories
5.1. AddRequestHeader GatewayFilter Factory
5.2. AddRequestParameter GatewayFilter Factory
5.3. AddResponseHeader GatewayFilter Factory
5.4. DedupeResponseHeader GatewayFilter Factory
5.5. Hystrix GatewayFilter Factory
5.6. FallbackHeaders GatewayFilter Factory
5.7. MapRequestHeader GatewayFilter Factory
5.8. PrefixPath GatewayFilter Factory
5.9. PreserveHostHeader GatewayFilter Factory
5.10. RequestRateLimiter GatewayFilter Factory
5.10.1. Redis RateLimiter
5.11. RedirectTo GatewayFilter Factory
5.12. RemoveHopByHopHeadersFilter GatewayFilter Factory
5.13. RemoveRequestHeader GatewayFilter Factory
5.14. RemoveResponseHeader GatewayFilter Factory
5.15. RewritePath GatewayFilter Factory
5.16. RewriteLocationResponseHeader GatewayFilter Factory
5.17. RewriteResponseHeader GatewayFilter Factory
5.18. SaveSession GatewayFilter Factory
5.19. SecureHeaders GatewayFilter Factory
5.20. SetPath GatewayFilter Factory
5.21. SetRequestHeader GatewayFilter Factory
5.22. SetResponseHeader GatewayFilter Factory
5.23. SetStatus GatewayFilter Factory
5.24. StripPrefix GatewayFilter Factory
5.25. Retry GatewayFilter Factory
5.26. RequestSize GatewayFilter Factory
5.27. Modify Request Body GatewayFilter Factory
5.28. Modify Response Body GatewayFilter Factory
5.29. Default Filters
6. Global Filters
6.1. Combined Global Filter and GatewayFilter Ordering
6.2. Forward Routing Filter
6.3. LoadBalancerClient Filter
6.4. ReactiveLoadBalancerClientFilter
6.5. Netty Routing Filter
6.6. Netty Write Response Filter
6.7. RouteToRequestUrl Filter
6.8. Websocket Routing Filter
6.9. Gateway Metrics Filter
6.10. Marking An Exchange As Routed
7. TLS / SSL
7.1. TLS Handshake
8. Configuration
8.1. Fluent Java Routes API
8.2. DiscoveryClient Route Definition Locator
8.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
9. Reactor Netty Access Logs
10. CORS Configuration
11. Actuator API
11.1. Verbose Actuator Format
11.2. Retrieving route filters
11.2.1. Global Filters
11.2.2. Route Filters
11.3. Refreshing the route cache
11.4. Retrieving the routes defined in the gateway
11.5. Retrieving information about a particular route
11.6. Creating and deleting a particular route
11.7. Recap: list of all endpoints
12. Troubleshooting
12.1. Log Levels
12.2. Wiretap
13. Developer Guide
13.1. Writing Custom Route Predicate Factories
13.2. Writing Custom GatewayFilter Factories
13.3. Writing Custom Global Filters
13.4. Writing Custom Route Locators and Writers
14. Building a Simple Gateway Using Spring MVC or Webflux
\ No newline at end of file + Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Configuring Route Predicate Factories and Gateway Filter Factories
4.1. Shortcut Configuration
4.2. Fully Expanded Arguments
5. Route Predicate Factories
5.1. After Route Predicate Factory
5.2. Before Route Predicate Factory
5.3. Between Route Predicate Factory
5.4. Cookie Route Predicate Factory
5.5. Header Route Predicate Factory
5.6. Host Route Predicate Factory
5.7. Method Route Predicate Factory
5.8. Path Route Predicate Factory
5.9. Query Route Predicate Factory
5.10. RemoteAddr Route Predicate Factory
5.11. Weight Route Predicate Factory
5.11.1. Modifying the way remote addresses are resolved
6. GatewayFilter Factories
6.1. AddRequestHeader GatewayFilter Factory
6.2. AddRequestParameter GatewayFilter Factory
6.3. AddResponseHeader GatewayFilter Factory
6.4. DedupeResponseHeader GatewayFilter Factory
6.5. Hystrix GatewayFilter Factory
6.6. FallbackHeaders GatewayFilter Factory
6.7. MapRequestHeader GatewayFilter Factory
6.8. PrefixPath GatewayFilter Factory
6.9. PreserveHostHeader GatewayFilter Factory
6.10. RequestRateLimiter GatewayFilter Factory
6.10.1. Redis RateLimiter
6.11. RedirectTo GatewayFilter Factory
6.12. RemoveRequestHeader GatewayFilter Factory
6.13. RemoveResponseHeader GatewayFilter Factory
6.14. RewritePath GatewayFilter Factory
6.15. RewriteLocationResponseHeader GatewayFilter Factory
6.16. RewriteResponseHeader GatewayFilter Factory
6.17. SaveSession GatewayFilter Factory
6.18. SecureHeaders GatewayFilter Factory
6.19. SetPath GatewayFilter Factory
6.20. SetRequestHeader GatewayFilter Factory
6.21. SetResponseHeader GatewayFilter Factory
6.22. SetStatus GatewayFilter Factory
6.23. StripPrefix GatewayFilter Factory
6.24. Retry GatewayFilter Factory
6.25. RequestSize GatewayFilter Factory
6.26. Modify Request Body GatewayFilter Factory
6.27. Modify Response Body GatewayFilter Factory
6.28. Default Filters
7. Global Filters
7.1. Combined Global Filter and GatewayFilter Ordering
7.2. Forward Routing Filter
7.3. LoadBalancerClient Filter
7.4. ReactiveLoadBalancerClientFilter
7.5. Netty Routing Filter
7.6. Netty Write Response Filter
7.7. RouteToRequestUrl Filter
7.8. Websocket Routing Filter
7.9. Gateway Metrics Filter
7.10. Marking An Exchange As Routed
8. HttpHeadersFilters
8.1. Forwarded Headers Filter
8.2. RemoveHopByHop Headers Filter
8.3. XForwarded Headers Filter
9. TLS / SSL
9.1. TLS Handshake
10. Configuration
10.1. Fluent Java Routes API
10.2. DiscoveryClient Route Definition Locator
10.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
11. Reactor Netty Access Logs
12. CORS Configuration
13. Actuator API
13.1. Verbose Actuator Format
13.2. Retrieving route filters
13.2.1. Global Filters
13.2.2. Route Filters
13.3. Refreshing the route cache
13.4. Retrieving the routes defined in the gateway
13.5. Retrieving information about a particular route
13.6. Creating and deleting a particular route
13.7. Recap: list of all endpoints
14. Troubleshooting
14.1. Log Levels
14.2. Wiretap
15. Developer Guide
15.1. Writing Custom Route Predicate Factories
15.2. Writing Custom GatewayFilter Factories
15.3. Writing Custom Global Filters
16. Building a Simple Gateway Using Spring MVC or Webflux
\ No newline at end of file diff --git a/2.1.x/multi/multi_troubleshooting.html b/2.1.x/multi/multi_troubleshooting.html index be9272e6..b4e7d79a 100644 --- a/2.1.x/multi/multi_troubleshooting.html +++ b/2.1.x/multi/multi_troubleshooting.html @@ -1,8 +1,8 @@ - 12. Troubleshooting

12. Troubleshooting

12.1 Log Levels

Below are some useful loggers that contain valuable trouble shooting infomration at the DEBUG and TRACE levels.

  • org.springframework.cloud.gateway
  • org.springframework.http.server.reactive
  • org.springframework.web.reactive
  • org.springframework.boot.autoconfigure.web
  • reactor.netty
  • redisratelimiter

12.2 Wiretap

The Reactor Netty HttpClient and HttpServer can have wiretap enabled. When combined + 14. Troubleshooting

14. Troubleshooting

14.1 Log Levels

Below are some useful loggers that contain valuable trouble shooting infomration at the DEBUG and TRACE levels.

  • org.springframework.cloud.gateway
  • org.springframework.http.server.reactive
  • org.springframework.web.reactive
  • org.springframework.boot.autoconfigure.web
  • reactor.netty
  • redisratelimiter

14.2 Wiretap

The Reactor Netty HttpClient and HttpServer can have wiretap enabled. When combined with setting the reactor.netty log level to DEBUG or TRACE will enable logging of information such as headers and bodies sent and received across the wire. To enable this, set spring.cloud.gateway.httpserver.wiretap=true and/or spring.cloud.gateway.httpclient.wiretap=true for the HttpServer and HttpClient -respectively.

\ No newline at end of file +respectively.

\ No newline at end of file diff --git a/2.1.x/single/spring-cloud-gateway.html b/2.1.x/single/spring-cloud-gateway.html index 410b6784..2e6460ec 100644 --- a/2.1.x/single/spring-cloud-gateway.html +++ b/2.1.x/single/spring-cloud-gateway.html @@ -1,6 +1,6 @@ - Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Route Predicate Factories
4.1. After Route Predicate Factory
4.2. Before Route Predicate Factory
4.3. Between Route Predicate Factory
4.4. Cookie Route Predicate Factory
4.5. Header Route Predicate Factory
4.6. Host Route Predicate Factory
4.7. Method Route Predicate Factory
4.8. Path Route Predicate Factory
4.9. Query Route Predicate Factory
4.10. RemoteAddr Route Predicate Factory
4.10.1. Modifying the way remote addresses are resolved
5. GatewayFilter Factories
5.1. AddRequestHeader GatewayFilter Factory
5.2. AddRequestParameter GatewayFilter Factory
5.3. AddResponseHeader GatewayFilter Factory
5.4. DedupeResponseHeader GatewayFilter Factory
5.5. Hystrix GatewayFilter Factory
5.6. FallbackHeaders GatewayFilter Factory
5.7. MapRequestHeader GatewayFilter Factory
5.8. PrefixPath GatewayFilter Factory
5.9. PreserveHostHeader GatewayFilter Factory
5.10. RequestRateLimiter GatewayFilter Factory
5.10.1. Redis RateLimiter
5.11. RedirectTo GatewayFilter Factory
5.12. RemoveHopByHopHeadersFilter GatewayFilter Factory
5.13. RemoveRequestHeader GatewayFilter Factory
5.14. RemoveResponseHeader GatewayFilter Factory
5.15. RewritePath GatewayFilter Factory
5.16. RewriteLocationResponseHeader GatewayFilter Factory
5.17. RewriteResponseHeader GatewayFilter Factory
5.18. SaveSession GatewayFilter Factory
5.19. SecureHeaders GatewayFilter Factory
5.20. SetPath GatewayFilter Factory
5.21. SetRequestHeader GatewayFilter Factory
5.22. SetResponseHeader GatewayFilter Factory
5.23. SetStatus GatewayFilter Factory
5.24. StripPrefix GatewayFilter Factory
5.25. Retry GatewayFilter Factory
5.26. RequestSize GatewayFilter Factory
5.27. Modify Request Body GatewayFilter Factory
5.28. Modify Response Body GatewayFilter Factory
5.29. Default Filters
6. Global Filters
6.1. Combined Global Filter and GatewayFilter Ordering
6.2. Forward Routing Filter
6.3. LoadBalancerClient Filter
6.4. ReactiveLoadBalancerClientFilter
6.5. Netty Routing Filter
6.6. Netty Write Response Filter
6.7. RouteToRequestUrl Filter
6.8. Websocket Routing Filter
6.9. Gateway Metrics Filter
6.10. Marking An Exchange As Routed
7. TLS / SSL
7.1. TLS Handshake
8. Configuration
8.1. Fluent Java Routes API
8.2. DiscoveryClient Route Definition Locator
8.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
9. Reactor Netty Access Logs
10. CORS Configuration
11. Actuator API
11.1. Verbose Actuator Format
11.2. Retrieving route filters
11.2.1. Global Filters
11.2.2. Route Filters
11.3. Refreshing the route cache
11.4. Retrieving the routes defined in the gateway
11.5. Retrieving information about a particular route
11.6. Creating and deleting a particular route
11.7. Recap: list of all endpoints
12. Troubleshooting
12.1. Log Levels
12.2. Wiretap
13. Developer Guide
13.1. Writing Custom Route Predicate Factories
13.2. Writing Custom GatewayFilter Factories
13.3. Writing Custom Global Filters
13.4. Writing Custom Route Locators and Writers
14. Building a Simple Gateway Using Spring MVC or Webflux

2.1.5.BUILD-SNAPSHOT

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

1. How to Include Spring Cloud Gateway

To include Spring Cloud Gateway in your project use the starter with group org.springframework.cloud + Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Configuring Route Predicate Factories and Gateway Filter Factories
4.1. Shortcut Configuration
4.2. Fully Expanded Arguments
5. Route Predicate Factories
5.1. After Route Predicate Factory
5.2. Before Route Predicate Factory
5.3. Between Route Predicate Factory
5.4. Cookie Route Predicate Factory
5.5. Header Route Predicate Factory
5.6. Host Route Predicate Factory
5.7. Method Route Predicate Factory
5.8. Path Route Predicate Factory
5.9. Query Route Predicate Factory
5.10. RemoteAddr Route Predicate Factory
5.11. Weight Route Predicate Factory
5.11.1. Modifying the way remote addresses are resolved
6. GatewayFilter Factories
6.1. AddRequestHeader GatewayFilter Factory
6.2. AddRequestParameter GatewayFilter Factory
6.3. AddResponseHeader GatewayFilter Factory
6.4. DedupeResponseHeader GatewayFilter Factory
6.5. Hystrix GatewayFilter Factory
6.6. FallbackHeaders GatewayFilter Factory
6.7. MapRequestHeader GatewayFilter Factory
6.8. PrefixPath GatewayFilter Factory
6.9. PreserveHostHeader GatewayFilter Factory
6.10. RequestRateLimiter GatewayFilter Factory
6.10.1. Redis RateLimiter
6.11. RedirectTo GatewayFilter Factory
6.12. RemoveRequestHeader GatewayFilter Factory
6.13. RemoveResponseHeader GatewayFilter Factory
6.14. RewritePath GatewayFilter Factory
6.15. RewriteLocationResponseHeader GatewayFilter Factory
6.16. RewriteResponseHeader GatewayFilter Factory
6.17. SaveSession GatewayFilter Factory
6.18. SecureHeaders GatewayFilter Factory
6.19. SetPath GatewayFilter Factory
6.20. SetRequestHeader GatewayFilter Factory
6.21. SetResponseHeader GatewayFilter Factory
6.22. SetStatus GatewayFilter Factory
6.23. StripPrefix GatewayFilter Factory
6.24. Retry GatewayFilter Factory
6.25. RequestSize GatewayFilter Factory
6.26. Modify Request Body GatewayFilter Factory
6.27. Modify Response Body GatewayFilter Factory
6.28. Default Filters
7. Global Filters
7.1. Combined Global Filter and GatewayFilter Ordering
7.2. Forward Routing Filter
7.3. LoadBalancerClient Filter
7.4. ReactiveLoadBalancerClientFilter
7.5. Netty Routing Filter
7.6. Netty Write Response Filter
7.7. RouteToRequestUrl Filter
7.8. Websocket Routing Filter
7.9. Gateway Metrics Filter
7.10. Marking An Exchange As Routed
8. HttpHeadersFilters
8.1. Forwarded Headers Filter
8.2. RemoveHopByHop Headers Filter
8.3. XForwarded Headers Filter
9. TLS / SSL
9.1. TLS Handshake
10. Configuration
10.1. Fluent Java Routes API
10.2. DiscoveryClient Route Definition Locator
10.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
11. Reactor Netty Access Logs
12. CORS Configuration
13. Actuator API
13.1. Verbose Actuator Format
13.2. Retrieving route filters
13.2.1. Global Filters
13.2.2. Route Filters
13.3. Refreshing the route cache
13.4. Retrieving the routes defined in the gateway
13.5. Retrieving information about a particular route
13.6. Creating and deleting a particular route
13.7. Recap: list of all endpoints
14. Troubleshooting
14.1. Log Levels
14.2. Wiretap
15. Developer Guide
15.1. Writing Custom Route Predicate Factories
15.2. Writing Custom GatewayFilter Factories
15.3. Writing Custom Global Filters
16. Building a Simple Gateway Using Spring MVC or Webflux

2.1.5.BUILD-SNAPSHOT

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

1. How to Include Spring Cloud Gateway

To include Spring Cloud Gateway in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-gateway. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

If you include the starter, but, for some reason, you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false.

[Important]Important

Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or built as a WAR.

2. Glossary

  • Route: Route the basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates and a collection of filters. A route is matched if aggregate predicate is true.
  • Predicate: This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This allows developers to match on anything from the HTTP request, such as headers or parameters.
  • Filter: These are instances Spring Framework GatewayFilter constructed in with a specific factory. Here, requests and responses can be modified before or after sending the downstream request.

3. How It Works

Spring Cloud Gateway Diagram

Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler. This handler runs sends the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line, is that filters may execute logic before the proxy request is sent or after. All "pre" filter logic is executed, then the proxy request is made. After the proxy request is made, the "post" filter logic is executed.

[Note]Note

URIs defined in routes without a port will get a default port set to 80 and 443 for HTTP and HTTPS URIs respectively.

4. Route Predicate Factories

Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping infrastructure. Spring Cloud Gateway includes many built-in Route Predicate Factories. All of these predicates match on different attributes of the HTTP request. Multiple Route Predicate Factories can be combined and are combined via logical and.

4.1 After Route Predicate Factory

The After Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen after the current datetime.

application.yml.  +working with Spring Cloud Gateway.

[Important]Important

Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or built as a WAR.

2. Glossary

  • Route: Route the basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates and a collection of filters. A route is matched if aggregate predicate is true.
  • Predicate: This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This allows developers to match on anything from the HTTP request, such as headers or parameters.
  • Filter: These are instances Spring Framework GatewayFilter constructed in with a specific factory. Here, requests and responses can be modified before or after sending the downstream request.

3. How It Works

Spring Cloud Gateway Diagram

Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler. This handler runs sends the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line, is that filters may execute logic before the proxy request is sent or after. All "pre" filter logic is executed, then the proxy request is made. After the proxy request is made, the "post" filter logic is executed.

[Note]Note

URIs defined in routes without a port will get a default port set to 80 and 443 for HTTP and HTTPS URIs respectively.

4. Configuring Route Predicate Factories and Gateway Filter Factories

There are two ways to configure predicates and filters: shortcuts and fully expanded arguments. Most examples below use the shortcut way.

The name and argument names will be listed as code in the first sentance or two of the each section. The arguments are typically listed in the order that would be needed for the shortcut configuration.

4.1 Shortcut Configuration

Shortcut configuration is recognized by the filter name, followed by an equals sign (=), followed by argument values separated by commas (,).

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: after_route
+        uri: https://example.org
+        predicates:
+        - Cookie=mycookie,mycookievalue

+

The previous sample defines the Cookie Route Predicate Factory with two arguments, the cookie name, mycookie and the value to match mycookievalue.

4.2 Fully Expanded Arguments

Fully expanded arguments appear more like standard yaml configuration with name/value pairs. Typically, there will be a name key and an args key. The args key is a map of key value pairs to configure the predicate or filter.

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: after_route
+        uri: https://example.org
+        predicates:
+        - name: Cookie
+          args:
+            name: mycookie
+            regexp: mycookievalue

+

This is the full configuration of the shortcut configuration of the Cookie predicate shown above.

5. Route Predicate Factories

Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping infrastructure. Spring Cloud Gateway includes many built-in Route Predicate Factories. All of these predicates match on different attributes of the HTTP request. Multiple Route Predicate Factories can be combined and are combined via logical and.

5.1 After Route Predicate Factory

The After Route Predicate Factory takes one parameter, a datetime (which is a java ZonedDateTime). This predicate matches requests that happen after the current datetime.

application.yml. 

spring:
   cloud:
     gateway:
@@ -17,7 +38,7 @@ working with Spring Cloud Gateway.

uri: https://example.org predicates: - After=2017-01-20T17:42:47.789-07:00[America/Denver]

-

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver).

4.2 Before Route Predicate Factory

The Before Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen before the current datetime.

application.yml.  +

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver).

5.2 Before Route Predicate Factory

The Before Route Predicate Factory takes one parameter, a datetime(which is a java ZonedDateTime). This predicate matches requests that happen before the current datetime.

application.yml. 

spring:
   cloud:
     gateway:
@@ -26,7 +47,7 @@ working with Spring Cloud Gateway.

uri: https://example.org predicates: - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

-

This route matches any request before Jan 20, 2017 17:42 Mountain Time (Denver).

4.3 Between Route Predicate Factory

The Between Route Predicate Factory takes two parameters, datetime1 and datetime2. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1.

application.yml.  +

This route matches any request before Jan 20, 2017 17:42 Mountain Time (Denver).

5.3 Between Route Predicate Factory

The Between Route Predicate Factory takes two parameters, datetime1 and datetime2 which are java ZonedDateTime objects. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1.

application.yml. 

spring:
   cloud:
     gateway:
@@ -35,7 +56,7 @@ working with Spring Cloud Gateway.

uri: https://example.org predicates: - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

-

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver) and before Jan 21, 2017 17:42 Mountain Time (Denver). This could be useful for maintenance windows.

4.4 Cookie Route Predicate Factory

The Cookie Route Predicate Factory takes two parameters, the cookie name and a regular expression. This predicate matches cookies that have the given name and the value matches the regular expression.

application.yml.  +

This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver) and before Jan 21, 2017 17:42 Mountain Time (Denver). This could be useful for maintenance windows.

5.4 Cookie Route Predicate Factory

The Cookie Route Predicate Factory takes two parameters, the cookie name and a regexp (which is a Java regular expression). This predicate matches cookies that have the given name and the value matches the regular expression.

application.yml. 

spring:
   cloud:
     gateway:
@@ -44,7 +65,7 @@ working with Spring Cloud Gateway.

uri: https://example.org predicates: - Cookie=chocolate, ch.p

-

This route matches the request has a cookie named chocolate who’s value matches the ch.p regular expression.

4.5 Header Route Predicate Factory

The Header Route Predicate Factory takes two parameters, the header name and a regular expression. This predicate matches with a header that has the given name and the value matches the regular expression.

application.yml.  +

This route matches the request has a cookie named chocolate who’s value matches the ch.p regular expression.

5.5 Header Route Predicate Factory

The Header Route Predicate Factory takes two parameters, the header name and a regexp (which is a Java regular expression). This predicate matches with a header that has the given name and the value matches the regular expression.

application.yml. 

spring:
   cloud:
     gateway:
@@ -53,7 +74,7 @@ working with Spring Cloud Gateway.

uri: https://example.org predicates: - Header=X-Request-Id, \d+

-

This route matches if the request has a header named X-Request-Id whos value matches the \d+ regular expression (has a value of one or more digits).

4.6 Host Route Predicate Factory

The Host Route Predicate Factory takes one parameter: a list of host name patterns. The pattern is an Ant style pattern with . as the separator. This predicates matches the Host header that matches the pattern.

application.yml.  +

This route matches if the request has a header named X-Request-Id whos value matches the \d+ regular expression (has a value of one or more digits).

5.6 Host Route Predicate Factory

The Host Route Predicate Factory takes one parameter: a list of host name patterns. The pattern is an Ant style pattern with . as the separator. This predicates matches the Host header that matches the pattern.

application.yml. 

spring:
   cloud:
     gateway:
@@ -62,7 +83,7 @@ working with Spring Cloud Gateway.

uri: https://example.org predicates: - Host=**.somehost.org,**.anotherhost.org

-

URI template variables are supported as well, such as {sub}.myhost.org.

This route would match if the request has a Host header has the value www.somehost.org or beta.somehost.org or www.anotherhost.org.

This predicate extracts the URI template variables (like sub defined in the example above) as a map of names and values and places it in the ServerWebExchange.getAttributes() with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. Those values are then available for use by GatewayFilter Factories

4.7 Method Route Predicate Factory

The Method Route Predicate Factory takes one or more parameters: the HTTP methods to match.

application.yml.  +

URI template variables are supported as well, such as {sub}.myhost.org.

This route would match if the request has a Host header has the value www.somehost.org or beta.somehost.org or www.anotherhost.org.

This predicate extracts the URI template variables (like sub defined in the example above) as a map of names and values and places it in the ServerWebExchange.getAttributes() with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. Those values are then available for use by GatewayFilter Factories

5.7 Method Route Predicate Factory

The Method Route Predicate Factory takes a methods argument which is one or more HTTP methods to match.

application.yml. 

spring:
   cloud:
     gateway:
@@ -71,7 +92,7 @@ working with Spring Cloud Gateway.

uri: https://example.org predicates: - Method=GET,POST

-

This route would match if the request method was a GET or a POST.

4.8 Path Route Predicate Factory

The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher patterns and an optional flag to matchOptionalTrailingSeparator.

application.yml.  +

This route would match if the request method was a GET or a POST.

5.8 Path Route Predicate Factory

The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher patterns and an optional flag to matchOptionalTrailingSeparator.

application.yml. 

spring:
   cloud:
     gateway:
@@ -82,7 +103,7 @@ working with Spring Cloud Gateway.

- Path=/foo/{segment},/bar/{segment}

This route would match if the request path was, for example: /foo/1 or /foo/bar or /bar/baz.

This predicate extracts the URI template variables (like segment defined in the example above) as a map of names and values and places it in the ServerWebExchange.getAttributes() with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. Those values are then available for use by GatewayFilter Factories

A utility method is available to make access to these variables easier.

Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
 
-String segment = uriVariables.get("segment");

4.9 Query Route Predicate Factory

The Query Route Predicate Factory takes two parameters: a required param and an optional regexp.

application.yml.  +String segment = uriVariables.get("segment");

5.9 Query Route Predicate Factory

The Query Route Predicate Factory takes two parameters: a required param and an optional regexp (which is a Java regular expression).

application.yml. 

spring:
   cloud:
     gateway:
@@ -100,7 +121,7 @@ String segment = uriVariables.get(        uri: https://example.org
         predicates:
         - Query=foo, ba.

-

This route would match if the request contained a foo query parameter whose value matched the ba. regexp, so bar and baz would match.

4.10 RemoteAddr Route Predicate Factory

The RemoteAddr Route Predicate Factory takes a list (min size 1) of CIDR-notation (IPv4 or IPv6) strings, e.g. 192.168.0.1/16 (where 192.168.0.1 is an IP address and 16 is a subnet mask).

application.yml.  +

This route would match if the request contained a foo query parameter whose value matched the ba. regexp, so bar and baz would match.

5.10 RemoteAddr Route Predicate Factory

The RemoteAddr Route Predicate Factory takes a list (min size 1) of sources, which are CIDR-notation (IPv4 or IPv6) strings, e.g. 192.168.0.1/16 (where 192.168.0.1 is an IP address and 16 is a subnet mask).

application.yml. 

spring:
   cloud:
     gateway:
@@ -109,7 +130,20 @@ String segment = uriVariables.get(        uri: https://example.org
         predicates:
         - RemoteAddr=192.168.1.1/24

-

This route would match if the remote address of the request was, for example, 192.168.1.10.

4.10.1 Modifying the way remote addresses are resolved

By default the RemoteAddr Route Predicate Factory uses the remote address from the incoming request. +

This route would match if the remote address of the request was, for example, 192.168.1.10.

5.11 Weight Route Predicate Factory

The Weight Route Predicate Factory takes two arguments group and weight (an int). The weights are calculated per group.

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: weight_high
+        uri: https://weighthigh.org
+        predicates:
+        - Weight=group1, 8
+      - id: weight_low
+        uri: https://weightlow.org
+        predicates:
+        - Weight=group1, 2

+

This route would forward ~80% of traffic to https://weighthigh.org and ~20% of traffic to https://weighlow.org

5.11.1 Modifying the way remote addresses are resolved

By default the RemoteAddr Route Predicate Factory uses the remote address from the incoming request. This may not match the actual client IP address if Spring Cloud Gateway sits behind a proxy layer.

You can customize the way that the remote address is resolved by setting a custom RemoteAddressResolver. Spring Cloud Gateway comes with one non-default remote address resolver which is based off of the X-Forwarded-For header, XForwardedRemoteAddressResolver.

XForwardedRemoteAddressResolver has two static constructor methods which take different approaches to security:

XForwardedRemoteAddressResolver::trustAll returns a RemoteAddressResolver which always takes the first IP address found in the X-Forwarded-For header. This approach is vulnerable to spoofing, as a malicious client could set an initial value for the X-Forwarded-For which would be accepted by the resolver.

XForwardedRemoteAddressResolver::maxTrustedIndex takes an index which correlates to the number of trusted infrastructure running in front of Spring Cloud Gateway. @@ -125,7 +159,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i .route("proxied-route", r -> r.remoteAddr(resolver, "10.10.1.1", "10.10.1.1/24") .uri("https://downstream2") -)

5. GatewayFilter Factories

Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories.

NOTE For more detailed examples on how to use any of the following filters, take a look at the unit tests.

5.1 AddRequestHeader GatewayFilter Factory

The AddRequestHeader GatewayFilter Factory takes a name and value parameter.

application.yml.  +)

6. GatewayFilter Factories

Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories.

NOTE For more detailed examples on how to use any of the following filters, take a look at the unit tests.

6.1 AddRequestHeader GatewayFilter Factory

The AddRequestHeader GatewayFilter Factory takes a name and value parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -145,7 +179,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
         - Path=/foo/{segment}
         filters:
         - AddRequestHeader=X-Request-Foo, Bar-{segment}

-

5.2 AddRequestParameter GatewayFilter Factory

The AddRequestParameter GatewayFilter Factory takes a name and value parameter.

application.yml.  +

6.2 AddRequestParameter GatewayFilter Factory

The AddRequestParameter GatewayFilter Factory takes a name and value parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -165,7 +199,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
         - Host: {segment}.myhost.org
         filters:
         - AddRequestParameter=foo, bar-{segment}

-

5.3 AddResponseHeader GatewayFilter Factory

The AddResponseHeader GatewayFilter Factory takes a name and value parameter.

application.yml.  +

6.3 AddResponseHeader GatewayFilter Factory

The AddResponseHeader GatewayFilter Factory takes a name and value parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -185,7 +219,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
         - Host: {segment}.myhost.org
         filters:
         - AddResponseHeader=foo, bar-{segment}

-

5.4 DedupeResponseHeader GatewayFilter Factory

The DedupeResponseHeader GatewayFilter Factory takes a name parameter and an optional strategy parameter. name can contain a list of header names, space separated.

application.yml.  +

6.4 DedupeResponseHeader GatewayFilter Factory

The DedupeResponseHeader GatewayFilter Factory takes a name parameter and an optional strategy parameter. name can contain a list of header names, space separated.

application.yml. 

spring:
   cloud:
     gateway:
@@ -194,8 +228,8 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
         uri: https://example.org
         filters:
         - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin

-

This will remove duplicate values of Access-Control-Allow-Credentials and Access-Control-Allow-Origin response headers in cases when both the gateway CORS logic and the downstream add them.

The DedupeResponseHeader filter also accepts an optional strategy parameter. The accepted values are RETAIN_FIRST (default), RETAIN_LAST, and RETAIN_UNIQUE.

5.5 Hystrix GatewayFilter Factory

Hystrix is a library from Netflix that implements the circuit breaker pattern. -The Hystrix GatewayFilter allows you to introduce circuit breakers to your gateway routes, protecting your services from cascading failures and allowing you to provide fallback responses in the event of downstream failures.

To enable Hystrix GatewayFilters in your project, add a dependency on spring-cloud-starter-netflix-hystrix from Spring Cloud Netflix.

The Hystrix GatewayFilter Factory requires a single name parameter, which is the name of the HystrixCommand.

application.yml.  +

This will remove duplicate values of Access-Control-Allow-Credentials and Access-Control-Allow-Origin response headers in cases when both the gateway CORS logic and the downstream add them.

The DedupeResponseHeader filter also accepts an optional strategy parameter. The accepted values are RETAIN_FIRST (default), RETAIN_LAST, and RETAIN_UNIQUE.

6.5 Hystrix GatewayFilter Factory

Hystrix is a library from Netflix that implements the circuit breaker pattern. +The Hystrix GatewayFilter allows you to introduce circuit breakers to your gateway routes, protecting your services from cascading failures and allowing you to provide fallback responses in the event of downstream failures.

To enable Hystrix GatewayFilters in your project, add a dependency on spring-cloud-starter-netflix-hystrix from Spring Cloud Netflix.

The Hystrix GatewayFilter Factory requires a single name parameter, which is the name of the HystrixCommand.

application.yml. 

spring:
   cloud:
     gateway:
@@ -243,9 +277,9 @@ app, registered under ServerWebExchange as the
 ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR attribute that can be used when
 handling the fallback within the gateway app.

For the external controller/ handler scenario, headers can be added with exception details. You can find more information -on it in the FallbackHeaders GatewayFilter Factory section.

Hystrix settings (such as timeouts) can be configured with global defaults or on a route by route basis using application properties as explained on the Hystrix wiki.

To set a 5 second timeout for the example route above, the following configuration would be used:

application.yml.  +on it in the FallbackHeaders GatewayFilter Factory section.

Hystrix settings (such as timeouts) can be configured with global defaults or on a route by route basis using application properties as explained on the Hystrix wiki.

To set a 5 second timeout for the example route above, the following configuration would be used:

application.yml. 

hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000

-

5.6 FallbackHeaders GatewayFilter Factory

The FallbackHeaders factory allows you to add Hystrix execution exception details in headers of a request forwarded to +

6.6 FallbackHeaders GatewayFilter Factory

The FallbackHeaders factory allows you to add Hystrix execution exception details in headers of a request forwarded to a fallbackUri in an external application, like in the following scenario:

application.yml. 

spring:
   cloud:
@@ -271,7 +305,7 @@ a fallbackUri in an external application, like in t
 

In this example, after an execution exception occurs while running the HystrixCommand, the request will be forwarde to the fallback endpoint or handler in an app running on localhost:9994. The headers with the exception type, message and -if available- root cause exception type and message will be added to that request by the FallbackHeaders filter.

The names of the headers can be overwritten in the config by setting the values of the arguments listed below, along with -their default values:

  • executionExceptionTypeHeaderName ("Execution-Exception-Type")
  • executionExceptionMessageHeaderName ("Execution-Exception-Message")
  • rootCauseExceptionTypeHeaderName ("Root-Cause-Exception-Type")
  • rootCauseExceptionMessageHeaderName ("Root-Cause-Exception-Message")

You can find more information on how Hystrix works with Gateway in the Hystrix GatewayFilter Factory section.

5.7 MapRequestHeader GatewayFilter Factory

The MapRequestHeader GatewayFilter Factory takes 'fromHeader' and 'toHeader' parameters. It creates a new named header (toHeader) and the value is extracted out of an existing named header (fromHeader) from the incoming http request. If the input header does not exist then the filter has no impact. If the new named header already exists then it’s values will be augmented with the new values.

application.yml.  +their default values:

  • executionExceptionTypeHeaderName ("Execution-Exception-Type")
  • executionExceptionMessageHeaderName ("Execution-Exception-Message")
  • rootCauseExceptionTypeHeaderName ("Root-Cause-Exception-Type")
  • rootCauseExceptionMessageHeaderName ("Root-Cause-Exception-Message")

You can find more information on how Hystrix works with Gateway in the Hystrix GatewayFilter Factory section.

6.7 MapRequestHeader GatewayFilter Factory

The MapRequestHeader GatewayFilter Facstory takes 'fromHeader' and 'toHeader' parameters. It creates a new named header (toHeader) and the value is extracted out of an existing named header (fromHeader) from the incoming http request. If the input header does not exist then the filter has no impact. If the new named header already exists then it’s values will be augmented with the new values.

application.yml. 

spring:
   cloud:
     gateway:
@@ -280,7 +314,7 @@ their default values:

    uri: https://example.org filters: - MapRequestHeader=Bar, X-Request-Foo

-

This will add X-Request-Foo:<values> header to the downstream request’s with updated values from the incoming http request Bar header.

5.8 PrefixPath GatewayFilter Factory

The PrefixPath GatewayFilter Factory takes a single prefix parameter.

application.yml.  +

This will add X-Request-Foo:<values> header to the downstream request’s with updated values from the incoming http request Bar header.

6.8 PrefixPath GatewayFilter Factory

The PrefixPath GatewayFilter Factory takes a single prefix parameter.

application.yml. 

spring:
   cloud:
     gateway:
@@ -289,7 +323,7 @@ their default values:

    uri: https://example.org filters: - PrefixPath=/mypath

-

This will prefix /mypath to the path of all matching requests. So a request to /hello, would be sent to /mypath/hello.

5.9 PreserveHostHeader GatewayFilter Factory

The PreserveHostHeader GatewayFilter Factory has not parameters. This filter, sets a request attribute that the routing filter will inspect to determine if the original host header should be sent, rather than the host header determined by the http client.

application.yml.  +

This will prefix /mypath to the path of all matching requests. So a request to /hello, would be sent to /mypath/hello.

6.9 PreserveHostHeader GatewayFilter Factory

The PreserveHostHeader GatewayFilter Factory has no parameters. This filter, sets a request attribute that the routing filter will inspect to determine if the original host header should be sent, rather than the host header determined by the http client.

application.yml. 

spring:
   cloud:
     gateway:
@@ -298,14 +332,14 @@ their default values:

    uri: https://example.org filters: - PreserveHostHeader

-

5.10 RequestRateLimiter GatewayFilter Factory

The RequestRateLimiter GatewayFilter Factory is uses a RateLimiter implementation to determine if the current request is allowed to proceed. If it is not, a status of HTTP 429 - Too Many Requests (by default) is returned.

This filter takes an optional keyResolver parameter and parameters specific to the rate limiter (see below).

keyResolver is a bean that implements the KeyResolver interface. In configuration, reference the bean by name using SpEL. #{@myKeyResolver} is a SpEL expression referencing a bean with the name myKeyResolver.

KeyResolver.java.  +

6.10 RequestRateLimiter GatewayFilter Factory

The RequestRateLimiter GatewayFilter Factory is uses a RateLimiter implementation to determine if the current request is allowed to proceed. If it is not, a status of HTTP 429 - Too Many Requests (by default) is returned.

This filter takes an optional keyResolver parameter and parameters specific to the rate limiter (see below).

keyResolver is a bean that implements the KeyResolver interface. In configuration, reference the bean by name using SpEL. #{@myKeyResolver} is a SpEL expression referencing a bean with the name myKeyResolver.

KeyResolver.java. 

public interface KeyResolver {
 	Mono<String> resolve(ServerWebExchange exchange);
 }

The KeyResolver interface allows pluggable strategies to derive the key for limiting requests. In future milestones, there will be some KeyResolver implementations.

The default implementation of KeyResolver is the PrincipalNameKeyResolver which retrieves the Principal from the ServerWebExchange and calls Principal.getName().

By default, if the KeyResolver does not find a key, requests will be denied. This behavior can be adjust with the spring.cloud.gateway.filter.request-rate-limiter.deny-empty-key (true or false) and spring.cloud.gateway.filter.request-rate-limiter.empty-key-status-code properties.

[Note]Note

The RequestRateLimiter is not configurable via the "shortcut" notation. The example below is invalid

application.properties. 

# INVALID SHORTCUT CONFIGURATION
 spring.cloud.gateway.routes[0].filters[0]=RequestRateLimiter=2, 2, #{@userkeyresolver}

-

5.10.1 Redis RateLimiter

The redis implementation is based off of work done at Stripe. It requires the use of the spring-boot-starter-data-redis-reactive Spring Boot starter.

The algorithm used is the Token Bucket Algorithm.

The redis-rate-limiter.replenishRate is how many requests per second do you want a user to be allowed to do, without any dropped requests. This is the rate that the token bucket is filled.

The redis-rate-limiter.burstCapacity is the maximum number of requests a user is allowed to do in a single second. This is the number of tokens the token bucket can hold. Setting this value to zero will block all requests.

A steady rate is accomplished by setting the same value in replenishRate and burstCapacity. Temporary bursts can be allowed by setting burstCapacity higher than replenishRate. In this case, the rate limiter needs to be allowed some time between bursts (according to replenishRate), as 2 consecutive bursts will result in dropped requests (HTTP 429 - Too Many Requests).

application.yml.  +

6.10.1 Redis RateLimiter

The redis implementation is based off of work done at Stripe. It requires the use of the spring-boot-starter-data-redis-reactive Spring Boot starter.

The algorithm used is the Token Bucket Algorithm.

The redis-rate-limiter.replenishRate is how many requests per second do you want a user to be allowed to do, without any dropped requests. This is the rate that the token bucket is filled.

The redis-rate-limiter.burstCapacity is the maximum number of requests a user is allowed to do in a single second. This is the number of tokens the token bucket can hold. Setting this value to zero will block all requests.

A steady rate is accomplished by setting the same value in replenishRate and burstCapacity. Temporary bursts can be allowed by setting burstCapacity higher than replenishRate. In this case, the rate limiter needs to be allowed some time between bursts (according to replenishRate), as 2 consecutive bursts will result in dropped requests (HTTP 429 - Too Many Requests).

application.yml. 

spring:
   cloud:
     gateway:
@@ -334,7 +368,7 @@ KeyResolver userKeyResolver() {
           args:
             rate-limiter: "#{@myRateLimiter}"
             key-resolver: "#{@userKeyResolver}"

-

5.11 RedirectTo GatewayFilter Factory

The RedirectTo GatewayFilter Factory takes a status and a url parameter. The status should be a 300 series redirect http code, such as 301. The url should be a valid url. This will be the value of the Location header.

application.yml.  +

6.11 RedirectTo GatewayFilter Factory

The RedirectTo GatewayFilter Factory takes a status and a url parameter. The status should be a 300 series redirect http code, such as 301. The url should be a valid url. This will be the value of the Location header.

application.yml. 

spring:
   cloud:
     gateway:
@@ -343,7 +377,7 @@ KeyResolver userKeyResolver() {
         uri: https://example.org
         filters:
         - RedirectTo=302, https://acme.org

-

This will send a status 302 with a Location:https://acme.org header to perform a redirect.

5.12 RemoveHopByHopHeadersFilter GatewayFilter Factory

The RemoveHopByHopHeadersFilter GatewayFilter Factory removes headers from forwarded requests. The default list of headers that is removed comes from the IETF.

The default removed headers are:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailer
  • Transfer-Encoding
  • Upgrade

To change this, set the spring.cloud.gateway.filter.remove-non-proxy-headers.headers property to the list of header names to remove.

5.13 RemoveRequestHeader GatewayFilter Factory

The RemoveRequestHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml.  +

This will send a status 302 with a Location:https://acme.org header to perform a redirect.

6.12 RemoveRequestHeader GatewayFilter Factory

The RemoveRequestHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml. 

spring:
   cloud:
     gateway:
@@ -352,7 +386,7 @@ KeyResolver userKeyResolver() {
         uri: https://example.org
         filters:
         - RemoveRequestHeader=X-Request-Foo

-

This will remove the X-Request-Foo header before it is sent downstream.

5.14 RemoveResponseHeader GatewayFilter Factory

The RemoveResponseHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml.  +

This will remove the X-Request-Foo header before it is sent downstream.

6.13 RemoveResponseHeader GatewayFilter Factory

The RemoveResponseHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed.

application.yml. 

spring:
   cloud:
     gateway:
@@ -363,7 +397,7 @@ KeyResolver userKeyResolver() {
         - RemoveResponseHeader=X-Response-Foo

This will remove the X-Response-Foo header from the response before it is returned to the gateway client.

To remove any kind of sensitive header you should configure this filter for any routes that you may want to do so. In addition you can configure this filter once using spring.cloud.gateway.default-filters -and have it applied to all routes.

5.15 RewritePath GatewayFilter Factory

The RewritePath GatewayFilter Factory takes a path regexp parameter and a replacement parameter. This uses Java regular expressions for a flexible way to rewrite the request path.

application.yml.  +and have it applied to all routes.

6.14 RewritePath GatewayFilter Factory

The RewritePath GatewayFilter Factory takes a path regexp parameter and a replacement parameter. This uses Java regular expressions for a flexible way to rewrite the request path.

application.yml. 

spring:
   cloud:
     gateway:
@@ -374,7 +408,7 @@ and have it applied to all routes.

- Path=/foo/** filters: - RewritePath=/foo(?<segment>/?.*), $\{segment}

-

For a request path of /foo/bar, this will set the path to /bar before making the downstream request. Notice the $\ which is replaced with $ because of the YAML spec.

5.16 RewriteLocationResponseHeader GatewayFilter Factory

The RewriteLocationResponseHeader GatewayFilter Factory modifies the value of Location response header, usually to get rid of backend specific details. It takes stripVersionMode, locationHeaderName, hostValue, and protocolsRegex parameters.

application.yml.  +

For a request path of /foo/bar, this will set the path to /bar before making the downstream request. Notice the $\ which is replaced with $ because of the YAML spec.

6.15 RewriteLocationResponseHeader GatewayFilter Factory

The RewriteLocationResponseHeader GatewayFilter Factory modifies the value of Location response header, usually to get rid of backend specific details. It takes stripVersionMode, locationHeaderName, hostValue, and protocolsRegex parameters.

application.yml. 

spring:
   cloud:
     gateway:
@@ -383,7 +417,7 @@ and have it applied to all routes.

uri: http://example.org filters: - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,

-

For example, for a request POST https://api.example.com/some/object/name, Location response header value https://object-service.prod.example.net/v2/some/object/id will be rewritten as https://api.example.com/some/object/id.

Parameter stripVersionMode has the following possible values: NEVER_STRIP, AS_IN_REQUEST (default), ALWAYS_STRIP.

  • NEVER_STRIP - Version will not be stripped, even if the original request path contains no version
  • AS_IN_REQUEST - Version will be stripped only if the original request path contains no version
  • ALWAYS_STRIP - Version will be stripped, even if the original request path contains version

Parameter hostValue, if provided, will be used to replace the host:port portion of the response Location header. If not provided, the value of the Host request header will be used.

Parameter protocolsRegex must be a valid regex String, against which the protocol name will be matched. If not matched, the filter will do nothing. Default is http|https|ftp|ftps.

5.17 RewriteResponseHeader GatewayFilter Factory

The RewriteResponseHeader GatewayFilter Factory takes name, regexp, and replacement parameters. It uses Java regular expressions for a flexible way to rewrite the response header value.

application.yml.  +

For example, for a request POST https://api.example.com/some/object/name, Location response header value https://object-service.prod.example.net/v2/some/object/id will be rewritten as https://api.example.com/some/object/id.

Parameter stripVersionMode has the following possible values: NEVER_STRIP, AS_IN_REQUEST (default), ALWAYS_STRIP.

  • NEVER_STRIP - Version will not be stripped, even if the original request path contains no version
  • AS_IN_REQUEST - Version will be stripped only if the original request path contains no version
  • ALWAYS_STRIP - Version will be stripped, even if the original request path contains version

Parameter hostValue, if provided, will be used to replace the host:port portion of the response Location header. If not provided, the value of the Host request header will be used.

Parameter protocolsRegex must be a valid regex String, against which the protocol name will be matched. If not matched, the filter will do nothing. Default is http|https|ftp|ftps.

6.16 RewriteResponseHeader GatewayFilter Factory

The RewriteResponseHeader GatewayFilter Factory takes name, regexp, and replacement parameters. It uses Java regular expressions for a flexible way to rewrite the response header value.

application.yml. 

spring:
   cloud:
     gateway:
@@ -392,7 +426,7 @@ and have it applied to all routes.

uri: https://example.org filters: - RewriteResponseHeader=X-Response-Foo, , password=[^&]+, password=***

-

For a header value of /42?user=ford&password=omg!what&flag=true, it will be set to /42?user=ford&password=***&flag=true after making the downstream request. Please use $\ to mean $ because of the YAML spec.

5.18 SaveSession GatewayFilter Factory

The SaveSession GatewayFilter Factory forces a WebSession::save operation before forwarding the call downstream. This is of particular use when +

For a header value of /42?user=ford&password=omg!what&flag=true, it will be set to /42?user=ford&password=***&flag=true after making the downstream request. Please use $\ to mean $ because of the YAML spec.

6.17 SaveSession GatewayFilter Factory

The SaveSession GatewayFilter Factory forces a WebSession::save operation before forwarding the call downstream. This is of particular use when using something like Spring Session with a lazy data store and need to ensure the session state has been saved before making the forwarded call.

application.yml. 

spring:
   cloud:
@@ -404,7 +438,7 @@ using something like         - Path=/foo/**
         filters:
         - SaveSession

-

If you are integrating Spring Security with Spring Session, and want to ensure security details have been forwarded to the remote process, this is critical.

5.19 SecureHeaders GatewayFilter Factory

The SecureHeaders GatewayFilter Factory adds a number of headers to the response at the recommendation from this blog post.

The following headers are added (along with default values):

  • X-Xss-Protection:1; mode=block
  • Strict-Transport-Security:max-age=631138519
  • X-Frame-Options:DENY
  • X-Content-Type-Options:nosniff
  • Referrer-Policy:no-referrer
  • Content-Security-Policy:default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'
  • X-Download-Options:noopen
  • X-Permitted-Cross-Domain-Policies:none

To change the default values set the appropriate property in the spring.cloud.gateway.filter.secure-headers namespace:

Property to change:

  • xss-protection-header
  • strict-transport-security
  • frame-options
  • content-type-options
  • referrer-policy
  • content-security-policy
  • download-options
  • permitted-cross-domain-policies

To disable the default values set the property spring.cloud.gateway.filter.secure-headers.disable with comma separated values.

[Note]Note

Need use lowercase and full name of secure headers.

The following values can use:

  • x-xss-protection
  • strict-transport-security
  • x-frame-options
  • x-content-type-options
  • referrer-policy
  • content-security-policy
  • x-download-options
  • x-permitted-cross-domain-policies

Example: spring.cloud.gateway.filter.secure-headers.disable=x-frame-options,strict-transport-security

5.20 SetPath GatewayFilter Factory

The SetPath GatewayFilter Factory takes a path template parameter. It offers a simple way to manipulate the request path by allowing templated segments of the path. This uses the uri templates from Spring Framework. Multiple matching segments are allowed.

application.yml.  +

If you are integrating Spring Security with Spring Session, and want to ensure security details have been forwarded to the remote process, this is critical.

6.18 SecureHeaders GatewayFilter Factory

The SecureHeaders GatewayFilter Factory adds a number of headers to the response at the recommendation from this blog post.

The following headers are added (along with default values):

  • X-Xss-Protection:1; mode=block
  • Strict-Transport-Security:max-age=631138519
  • X-Frame-Options:DENY
  • X-Content-Type-Options:nosniff
  • Referrer-Policy:no-referrer
  • Content-Security-Policy:default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'
  • X-Download-Options:noopen
  • X-Permitted-Cross-Domain-Policies:none

To change the default values set the appropriate property in the spring.cloud.gateway.filter.secure-headers namespace:

Property to change:

  • xss-protection-header
  • strict-transport-security
  • frame-options
  • content-type-options
  • referrer-policy
  • content-security-policy
  • download-options
  • permitted-cross-domain-policies

To disable the default values set the property spring.cloud.gateway.filter.secure-headers.disable with comma separated values.

[Note]Note

Need use lowercase and full name of secure headers.

The following values can use:

  • x-xss-protection
  • strict-transport-security
  • x-frame-options
  • x-content-type-options
  • referrer-policy
  • content-security-policy
  • x-download-options
  • x-permitted-cross-domain-policies

Example: spring.cloud.gateway.filter.secure-headers.disable=x-frame-options,strict-transport-security

6.19 SetPath GatewayFilter Factory

The SetPath GatewayFilter Factory takes a path template parameter. It offers a simple way to manipulate the request path by allowing templated segments of the path. This uses the uri templates from Spring Framework. Multiple matching segments are allowed.

application.yml. 

spring:
   cloud:
     gateway:
@@ -415,7 +449,7 @@ using something like         - Path=/foo/{segment}
         filters:
         - SetPath=/{segment}

-

For a request path of /foo/bar, this will set the path to /bar before making the downstream request.

5.21 SetRequestHeader GatewayFilter Factory

The SetRequestHeader GatewayFilter Factory takes name and value parameters.

application.yml.  +

For a request path of /foo/bar, this will set the path to /bar before making the downstream request.

6.20 SetRequestHeader GatewayFilter Factory

The SetRequestHeader GatewayFilter Factory takes name and value parameters.

application.yml. 

spring:
   cloud:
     gateway:
@@ -435,7 +469,7 @@ using something like         - Host: {segment}.myhost.org
         filters:
         - SetRequestHeader=foo, bar-{segment}

-

5.22 SetResponseHeader GatewayFilter Factory

The SetResponseHeader GatewayFilter Factory takes name and value parameters.

application.yml.  +

6.21 SetResponseHeader GatewayFilter Factory

The SetResponseHeader GatewayFilter Factory takes name and value parameters.

application.yml. 

spring:
   cloud:
     gateway:
@@ -455,7 +489,7 @@ using something like         - Host: {segment}.myhost.org
         filters:
         - SetResponseHeader=foo, bar-{segment}

-

5.23 SetStatus GatewayFilter Factory

The SetStatus GatewayFilter Factory takes a single status parameter. It must be a valid Spring HttpStatus. It may be the integer value 404 or the string representation of the enumeration NOT_FOUND.

application.yml.  +

6.22 SetStatus GatewayFilter Factory

The SetStatus GatewayFilter Factory takes a single status parameter. It must be a valid Spring HttpStatus. It may be the integer value 404 or the string representation of the enumeration NOT_FOUND.

application.yml. 

spring:
   cloud:
     gateway:
@@ -468,7 +502,7 @@ using something like         uri: https://example.org
         filters:
         - SetStatus=401

-

In either case, the HTTP status of the response will be set to 401.

5.24 StripPrefix GatewayFilter Factory

The StripPrefix GatewayFilter Factory takes one paramter, parts. The parts parameter indicated the number of parts in the path to strip from the request before sending it downstream.

application.yml.  +

In either case, the HTTP status of the response will be set to 401.

6.23 StripPrefix GatewayFilter Factory

The StripPrefix GatewayFilter Factory takes one paramter, parts. The parts parameter indicated the number of parts in the path to strip from the request before sending it downstream.

application.yml. 

spring:
   cloud:
     gateway:
@@ -479,7 +513,7 @@ using something like         - Path=/name/**
         filters:
         - StripPrefix=2

-

When a request is made through the gateway to /name/bar/foo the request made to nameservice will look like http://nameservice/foo.

5.25 Retry GatewayFilter Factory

The Retry GatewayFilter Factory support following set of parameters:

  • retries: the number of retries that should be attempted
  • statuses: the HTTP status codes that should be retried, represented using org.springframework.http.HttpStatus
  • methods: the HTTP methods that should be retried, represented using org.springframework.http.HttpMethod
  • series: the series of status codes to be retried, represented using org.springframework.http.HttpStatus.Series
  • exceptions: list of exceptions thrown that should be retried
  • backoff: configured exponential backoff for the retries. Retries are performed after a backoff interval of firstBackoff * (factor ^ n) where n is the iteration. +

    When a request is made through the gateway to /name/bar/foo the request made to nameservice will look like http://nameservice/foo.

6.24 Retry GatewayFilter Factory

The Retry GatewayFilter Factory support following set of parameters:

  • retries: the number of retries that should be attempted
  • statuses: the HTTP status codes that should be retried, represented using org.springframework.http.HttpStatus
  • methods: the HTTP methods that should be retried, represented using org.springframework.http.HttpMethod
  • series: the series of status codes to be retried, represented using org.springframework.http.HttpStatus.Series
  • exceptions: list of exceptions thrown that should be retried
  • backoff: configured exponential backoff for the retries. Retries are performed after a backoff interval of firstBackoff * (factor ^ n) where n is the iteration. If maxBackoff is configured, the maximum backoff applied will be limited to maxBackoff. If basedOnPreviousValue is true, backoff will be calculated using prevBackoff * factor.

The following defaults are configured for Retry filter if enabled:

  • retries — 3 times
  • series — 5XX series
  • methods — GET method
  • exceptions — IOException and TimeoutException
  • backoff — disabled

application.yml. 

spring:
@@ -495,12 +529,13 @@ If basedOnPreviousValue is true, backoff will be ca
           args:
             retries: 3
             statuses: BAD_GATEWAY
+            methods: GET,POST
             backoff:
               firstBackoff: 10ms
               maxBackoff: 50ms
               factor: 2
               basedOnPreviousValue: false

-

[Note]Note

The retry filter does not currently support retrying with a body (e.g. for POST or PUT requests with a body).

[Note]Note

When using the retry filter with a forward: prefixed URL, the target endpoint should be written carefully so that in case of an error it does not do anything that could result in a response being sent to the client and committed. For example, if the target endpoint is an annotated controller, the target controller method should not return ResponseEntity with an error status code. Instead it should throw an Exception, or signal an error, e.g. via a Mono.error(ex) return value, which the retry filter can be configured to handle by retrying.

5.26 RequestSize GatewayFilter Factory

The RequestSize GatewayFilter Factory can restrict a request from reaching the downstream service , when the request size is greater than the permissible limit. The filter takes RequestSize as parameter which is the permissible size limit of the request defined in bytes.

application.yml.  +

[Note]Note

When using the retry filter with a forward: prefixed URL, the target endpoint should be written carefully so that in case of an error it does not do anything that could result in a response being sent to the client and committed. For example, if the target endpoint is an annotated controller, the target controller method should not return ResponseEntity with an error status code. Instead it should throw an Exception, or signal an error, e.g. via a Mono.error(ex) return value, which the retry filter can be configured to handle by retrying.

[Warning]Warning

When using the retry filter with any HTTP method with a body, the body will be cached and the gateway will become memory constrained. The body is cached in a request attribute defined by ServerWebExchangeUtils.CACHED_REQUEST_BODY_ATTR. The type of the object is a org.springframework.core.io.buffer.DataBuffer.

6.25 RequestSize GatewayFilter Factory

The RequestSize GatewayFilter Factory can restrict a request from reaching the downstream service , when the request size is greater than the permissible limit. The filter takes a maxSize parameter which is the permissible size limit of the request. The maxSize is a `DataSize type, so values can be defined as a number followed by an optional DataUnit suffix such as 'KB' or 'MB'. The default is 'B' for bytes.

application.yml. 

spring:
   cloud:
     gateway:
@@ -513,7 +548,7 @@ If basedOnPreviousValue is true, backoff will be ca
       - name: RequestSize
         args:
           maxSize: 5000000

-

The RequestSize GatewayFilter Factory set the response status as 413 Payload Too Large with a additional header errorMessage when the Request is rejected due to size. Following is an example of such an errorMessage .

errorMessage : Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB

[Note]Note

The default Request size will be set to 5 MB if not provided as filter argument in route definition.

5.27 Modify Request Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

This filter can be used to modify the request body before it is sent downstream by the Gateway.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
+

The RequestSize GatewayFilter Factory set the response status as 413 Payload Too Large with a additional header errorMessage when the Request is rejected due to size. Following is an example of such an errorMessage .

errorMessage : Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB

[Note]Note

The default Request size will be set to 5 MB if not provided as filter argument in route definition.

6.26 Modify Request Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

The ModifyRequestBody filter can be used to modify the request body before it is sent downstream by the Gateway.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
 public RouteLocator routes(RouteLocatorBuilder builder) {
     return builder.routes()
         .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
@@ -539,7 +574,7 @@ If basedOnPreviousValue is true, backoff will be ca
     public void setMessage(String message) {
         this.message = message;
     }
-}

5.28 Modify Response Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

This filter can be used to modify the response body before it is sent back to the Client.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
+}

6.27 Modify Response Body GatewayFilter Factory

This filter is considered BETA and the API may change in the future

The ModifyResponseBody filter can be used to modify the response body before it is sent back to the Client.

[Note]Note

This filter can only be configured using the Java DSL

@Bean
 public RouteLocator routes(RouteLocatorBuilder builder) {
     return builder.routes()
         .route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
@@ -547,7 +582,7 @@ If basedOnPreviousValue is true, backoff will be ca
         		.modifyResponseBody(String.class, String.class,
         		    (exchange, s) -> Mono.just(s.toUpperCase()))).uri(uri)
         .build();
-}

5.29 Default Filters

If you would like to add a filter and apply it to all routes you can use spring.cloud.gateway.default-filters. +}

6.28 Default Filters

If you would like to add a filter and apply it to all routes you can use spring.cloud.gateway.default-filters. This property takes a list of filters

application.yml. 

spring:
   cloud:
@@ -555,7 +590,7 @@ This property takes a list of filters

application.yml.  default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar - PrefixPath=/httpbin

-

6. Global Filters

The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).

6.1 Combined Global Filter and GatewayFilter Ordering

When a request comes in (and matches a Route) the Filtering Web Handler will add all instances of GlobalFilter and all route specific instances of GatewayFilter to a filter chain. This combined filter chain is sorted by the org.springframework.core.Ordered interface, which can be set by implementing the getOrder() method.

As Spring Cloud Gateway distinguishes between "pre" and "post" phases for filter logic execution (see: How it Works), the filter with the highest precedence will be the first in the "pre"-phase and the last in the "post"-phase.

ExampleConfiguration.java.  +

7. Global Filters

The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).

7.1 Combined Global Filter and GatewayFilter Ordering

When a request comes in (and matches a Route) the Filtering Web Handler will add all instances of GlobalFilter and all route specific instances of GatewayFilter to a filter chain. This combined filter chain is sorted by the org.springframework.core.Ordered interface, which can be set by implementing the getOrder() method.

As Spring Cloud Gateway distinguishes between "pre" and "post" phases for filter logic execution (see: How it Works), the filter with the highest precedence will be the first in the "pre"-phase and the last in the "post"-phase.

ExampleConfiguration.java. 

@Bean
 public GlobalFilter customFilter() {
     return new CustomGlobalFilter();
@@ -574,7 +609,7 @@ This property takes a list of filters

application.yml.  return -1; } }

-

6.2 Forward Routing Filter

The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.

6.3 LoadBalancerClient Filter

The LoadBalancerClientFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud LoadBalancerClient to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. The filter will also look in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals lb and then the same rules apply.

application.yml.  +

7.2 Forward Routing Filter

The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.

7.3 LoadBalancerClient Filter

The LoadBalancerClientFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud LoadBalancerClient to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. The filter will also look in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals lb and then the same rules apply.

application.yml. 

spring:
   cloud:
     gateway:
@@ -590,9 +625,9 @@ but the ServiceInstance indicates it is not secure,
 HTTP.  The opposite situation can also apply.  However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the
 route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the
 route URL will override the ServiceInstance configuration.

[Warning]Warning

LoadBalancerClientFilter uses a blocking Ribbon LoadBalancerClient under the hood. -We suggest you use ReactiveLoadBalancerClientFilter instead. +We suggest you use ReactiveLoadBalancerClientFilter instead. You can switch to using it by adding org.springframework.cloud:spring-cloud-loadbalancer dependency to your project -and setting the value of the spring.cloud.loadbalancer.ribbon.enabled to false.

6.4 ReactiveLoadBalancerClientFilter

The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute +and setting the value of the spring.cloud.loadbalancer.ribbon.enabled to false.

7.4 ReactiveLoadBalancerClientFilter

The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), it will use the Spring Cloud ReactorLoadBalancer to resolve the name (myservice in the previous example) to an actual host and port and replace the URI in the same attribute. The unmodified @@ -613,7 +648,7 @@ the scheme specified in the request made to the Gateway. For example, if the re but the ServiceInstance indicates it is not secure, then the downstream request will be made over HTTP. The opposite situation can also apply. However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the -route URL will override the ServiceInstance configuration.

6.5 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

6.6 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

6.7 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

6.8 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml.  +route URL will override the ServiceInstance configuration.

7.5 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

7.6 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

7.7 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

7.8 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml. 

spring:
   cloud:
     gateway:
@@ -628,10 +663,10 @@ route URL will override the ServiceInstance configu
         uri: ws://localhost:3001
         predicates:
         - Path=/websocket/**

-

6.9 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

  • routeId: The route id
  • routeUri: The URI that the API will be routed to
  • outcome: Outcome as classified by HttpStatus.Series
  • status: Http Status of the request returned to the client
  • httpStatusCode: Http Status of the request returned to the client
  • httpMethod: The Http method used for the request

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

6.10 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted +

7.9 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

  • routeId: The route id
  • routeUri: The URI that the API will be routed to
  • outcome: Outcome as classified by HttpStatus.Series
  • status: Http Status of the request returned to the client
  • httpStatusCode: Http Status of the request returned to the client
  • httpMethod: The Http method used for the request

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

7.10 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted to the exchange attributes. Once a request has been marked as routed, other routing filters will not route the request again, essentially skipping the filter. There are convenience methods that you can use to mark an exchange as routed -or check if an exchange has already been routed.

  • ServerWebExchangeUtils.isAlreadyRouted takes a ServerWebExchange object and checks if it has been "routed"
  • ServerWebExchangeUtils.setAlreadyRouted takes a ServerWebExchange object and marks it as "routed"

7. TLS / SSL

The Gateway can listen for requests on https by following the usual Spring server configuration. Example:

application.yml.  +or check if an exchange has already been routed.

  • ServerWebExchangeUtils.isAlreadyRouted takes a ServerWebExchange object and checks if it has been "routed"
  • ServerWebExchangeUtils.setAlreadyRouted takes a ServerWebExchange object and marks it as "routed"

8. HttpHeadersFilters

HttpHeadersFilters are applied to requests before sending them downstream, such as in the NettyRoutingFilter.

8.1 Forwarded Headers Filter

The Forwarded Headers Filter creates a Forwarded header to send to the downstream service. It adds the Host header, scheme and port of the current request to any existing Forwarded header.

8.2 RemoveHopByHop Headers Filter

The RemoveHopByHop Headers Filter removes headers from forwarded requests. The default list of headers that is removed comes from the IETF.

The default removed headers are:

  • Connection
  • Keep-Alive
  • Proxy-Authenticate
  • Proxy-Authorization
  • TE
  • Trailer
  • Transfer-Encoding
  • Upgrade

To change this, set the spring.cloud.gateway.filter.remove-non-proxy-headers.headers property to the list of header names to remove.

8.3 XForwarded Headers Filter

The XForwarded Headers Filter creates various a X-Forwarded-* headers to send to the downstream service. It users the Host header, scheme, port and path of the current request to create the various headers.

Creating of individual headers can be controlled by the following boolean properties (defaults to true):

  • spring.cloud.gateway.x-forwarded.for.enabled
  • spring.cloud.gateway.x-forwarded.host.enabled
  • spring.cloud.gateway.x-forwarded.port.enabled
  • spring.cloud.gateway.x-forwarded.proto.enabled
  • spring.cloud.gateway.x-forwarded.prefix.enabled

Appending multiple headers can be controlled by the following boolean properties (defaults to true):

  • spring.cloud.gateway.x-forwarded.for.append
  • spring.cloud.gateway.x-forwarded.host.append
  • spring.cloud.gateway.x-forwarded.port.append
  • spring.cloud.gateway.x-forwarded.proto.append
  • spring.cloud.gateway.x-forwarded.prefix.append

9. TLS / SSL

The Gateway can listen for requests on https by following the usual Spring server configuration. Example:

application.yml. 

server:
   ssl:
     enabled: true
@@ -655,7 +690,7 @@ or check if an exchange has already been routed.

trustedX509Certificates: - cert1.pem - cert2.pem

-

If the Spring Cloud Gateway is not provisioned with trusted certificates the default trust store is used (which can be overriden with system property javax.net.ssl.trustStore).

7.1 TLS Handshake

The Gateway maintains a client pool that it uses to route to backends. When communicating over https the client initiates a TLS handshake. A number of timeouts are assoicated with this handshake. These timeouts can be configured (defaults shown):

application.yml.  +

If the Spring Cloud Gateway is not provisioned with trusted certificates the default trust store is used (which can be overriden with system property javax.net.ssl.trustStore).

9.1 TLS Handshake

The Gateway maintains a client pool that it uses to route to backends. When communicating over https the client initiates a TLS handshake. A number of timeouts are assoicated with this handshake. These timeouts can be configured (defaults shown):

application.yml. 

spring:
   cloud:
     gateway:
@@ -664,7 +699,7 @@ or check if an exchange has already been routed.

handshake-timeout-millis: 10000 close-notify-flush-timeout-millis: 3000 close-notify-read-timeout-millis: 0

-

8. Configuration

Configuration for Spring Cloud Gateway is driven by a collection of RouteDefinitionLocators.

RouteDefinitionLocator.java.  +

10. Configuration

Configuration for Spring Cloud Gateway is driven by a collection of RouteDefinitionLocators.

RouteDefinitionLocator.java. 

public interface RouteDefinitionLocator {
 	Flux<RouteDefinition> getRouteDefinitions();
 }

@@ -683,7 +718,7 @@ or check if an exchange has already been routed.

uri: https://example.org filters: - SetStatus=401

-

For some usages of the gateway, properties will be adequate, but some production use cases will benefit from loading configuration from an external source, such as a database. Future milestone versions will have RouteDefinitionLocator implementations based off of Spring Data Repositories such as: Redis, MongoDB and Cassandra.

8.1 Fluent Java Routes API

To allow for simple configuration in Java, there is a fluent API defined in the RouteLocatorBuilder bean.

GatewaySampleApplication.java.  +

For some usages of the gateway, properties will be adequate, but some production use cases will benefit from loading configuration from an external source, such as a database. Future milestone versions will have RouteDefinitionLocator implementations based off of Spring Data Repositories such as: Redis, MongoDB and Cassandra.

10.1 Fluent Java Routes API

To allow for simple configuration in Java, there is a fluent API defined in the RouteLocatorBuilder bean.

GatewaySampleApplication.java. 

// static imports from GatewayFilters and RoutePredicates
 @Bean
 public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
@@ -708,7 +743,7 @@ or check if an exchange has already been routed.

-

This style also allows for more custom predicate assertions. The predicates defined by RouteDefinitionLocator beans are combined using logical and. By using the fluent Java API, you can use the and(), or() and negate() operators on the Predicate class.

8.2 DiscoveryClient Route Definition Locator

The Gateway can be configured to create routes based on services registered with a DiscoveryClient compatible service registry.

To enable this, set spring.cloud.gateway.discovery.locator.enabled=true and make sure a DiscoveryClient implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper).

8.2.1 Configuring Predicates and Filters For DiscoveryClient Routes

By default the Gateway defines a single predicate and filter for routes created via a DiscoveryClient.

The default predicate is a path predicate defined with the pattern /serviceId/**, where serviceId is +

This style also allows for more custom predicate assertions. The predicates defined by RouteDefinitionLocator beans are combined using logical and. By using the fluent Java API, you can use the and(), or() and negate() operators on the Predicate class.

10.2 DiscoveryClient Route Definition Locator

The Gateway can be configured to create routes based on services registered with a DiscoveryClient compatible service registry.

To enable this, set spring.cloud.gateway.discovery.locator.enabled=true and make sure a DiscoveryClient implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper).

10.2.1 Configuring Predicates and Filters For DiscoveryClient Routes

By default the Gateway defines a single predicate and filter for routes created via a DiscoveryClient.

The default predicate is a path predicate defined with the pattern /serviceId/**, where serviceId is the id of the service from the DiscoveryClient.

The default filter is rewrite path filter with the regex /serviceId/(?<remaining>.*) and the replacement /${remaining}. This just strips the service id from the path before the request is sent downstream.

If you would like to customize the predicates and/or filters used by the DiscoveryClient routes you can do so @@ -724,7 +759,7 @@ spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'" spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"

-

9. Reactor Netty Access Logs

To enable Reactor Netty access logs, set -Dreactor.netty.http.server.accessLogEnabled=true. (It must be a Java System Property, not a Spring Boot property).

The logging system can be configured to have a separate access log file. Below is an example logback configuration:

logback.xml.  +

11. Reactor Netty Access Logs

To enable Reactor Netty access logs, set -Dreactor.netty.http.server.accessLogEnabled=true. (It must be a Java System Property, not a Spring Boot property).

The logging system can be configured to have a separate access log file. Below is an example logback configuration:

logback.xml. 

    <appender name="accessLog" class="ch.qos.logback.core.FileAppender">
         <file>access_log.log</file>
         <encoder>
@@ -738,7 +773,7 @@ spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remain
     <logger name="reactor.netty.http.server.AccessLog" level="INFO" additivity="false">
         <appender-ref ref="async"/>
     </logger>

-

10. CORS Configuration

The gateway can be configured to control CORS behavior. The "global" CORS configuration is a map of URL patterns to Spring Framework CorsConfiguration.

application.yml.  +

12. CORS Configuration

The gateway can be configured to control CORS behavior. The "global" CORS configuration is a map of URL patterns to Spring Framework CorsConfiguration.

application.yml. 

spring:
   cloud:
     gateway:
@@ -748,10 +783,10 @@ spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remain
             allowedOrigins: "https://docs.spring.io"
             allowedMethods:
             - GET

-

In the example above, CORS requests will be allowed from requests that originate from docs.spring.io for all GET requested paths.

To provide the same CORS configuration to requests that are not handled by some gateway route predicate, set the property spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping equal to true. This is useful when trying to support CORS preflight requests and your route predicate doesn’t evalute to true because the http method is options.

11. Actuator API

The /gateway actuator endpoint allows to monitor and interact with a Spring Cloud Gateway application. To be remotely accessible, the endpoint has to be enabled and exposed via HTTP or JMX in the application properties.

application.properties.  +

In the example above, CORS requests will be allowed from requests that originate from docs.spring.io for all GET requested paths.

To provide the same CORS configuration to requests that are not handled by some gateway route predicate, set the property spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping equal to true. This is useful when trying to support CORS preflight requests and your route predicate doesn’t evalute to true because the http method is options.

13. Actuator API

The /gateway actuator endpoint allows to monitor and interact with a Spring Cloud Gateway application. To be remotely accessible, the endpoint has to be enabled and exposed via HTTP or JMX in the application properties.

application.properties. 

management.endpoint.gateway.enabled=true # default value
 management.endpoints.web.exposure.include=gateway

-

11.1 Verbose Actuator Format

A new, more verbose format has been added to Gateway. This adds more detail to each route allowing to view the predicates and filters associated to each route along with any configuration that is available.

/actuator/gateway/routes

[
+

13.1 Verbose Actuator Format

A new, more verbose format has been added to Gateway. This adds more detail to each route allowing to view the predicates and filters associated to each route along with any configuration that is available.

/actuator/gateway/routes

[
   {
     "predicate": "(Hosts: [**.addrequestheader.org] && Paths: [/headers], match trailing slash: true)",
     "route_id": "add_request_header_test",
@@ -765,7 +800,7 @@ spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remain
   }
 ]

To enable this feature, set the following property:

application.properties. 

spring.cloud.gateway.actuator.verbose.enabled=true

-

This will default to true in a future release.

11.2 Retrieving route filters

11.2.1 Global Filters

To retrieve the global filters applied to all routes, make a GET request to /actuator/gateway/globalfilters. The resulting response is similar to the following:

{
+

This will default to true in a future release.

13.2 Retrieving route filters

13.2.1 Global Filters

To retrieve the global filters applied to all routes, make a GET request to /actuator/gateway/globalfilters. The resulting response is similar to the following:

{
   "org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5": 10100,
   "org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@4f6fd101": 10000,
   "org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@32d22650": -1,
@@ -774,11 +809,11 @@ spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remain
   "org.springframework.cloud.gateway.filter.ForwardPathFilter@33a71d23": 0,
   "org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@135064ea": 2147483637,
   "org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@23c05889": 2147483646
-}

The response contains details of the global filters in place. For each global filter is provided the string representation of the filter object (e.g., org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5) and the corresponding order in the filter chain.

11.2.2 Route Filters

To retrieve the GatewayFilter factories applied to routes, make a GET request to /actuator/gateway/routefilters. The resulting response is similar to the following:

{
+}

The response contains details of the global filters in place. For each global filter is provided the string representation of the filter object (e.g., org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@77856cc5) and the corresponding order in the filter chain.

13.2.2 Route Filters

To retrieve the GatewayFilter factories applied to routes, make a GET request to /actuator/gateway/routefilters. The resulting response is similar to the following:

{
   "[AddRequestHeaderGatewayFilterFactory@570ed9c configClass = AbstractNameValueGatewayFilterFactory.NameValueConfig]": null,
   "[SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]": null,
   "[SaveSessionGatewayFilterFactory@4449b273 configClass = Object]": null
-}

The response contains details of the GatewayFilter factories applied to any particular route. For each factory is provided the string representation of the corresponding object (e.g., [SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]). Note that the null value is due to an incomplete implementation of the endpoint controller, for that it tries to set the order of the object in the filter chain, which does not apply to a GatewayFilter factory object.

11.3 Refreshing the route cache

To clear the routes cache, make a POST request to /actuator/gateway/refresh. The request returns a 200 without response body.

11.4 Retrieving the routes defined in the gateway

To retrieve the routes defined in the gateway, make a GET request to /actuator/gateway/routes. The resulting response is similar to the following:

[{
+}

The response contains details of the GatewayFilter factories applied to any particular route. For each factory is provided the string representation of the corresponding object (e.g., [SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]). Note that the null value is due to an incomplete implementation of the endpoint controller, for that it tries to set the order of the object in the filter chain, which does not apply to a GatewayFilter factory object.

13.3 Refreshing the route cache

To clear the routes cache, make a POST request to /actuator/gateway/refresh. The request returns a 200 without response body.

13.4 Retrieving the routes defined in the gateway

To retrieve the routes defined in the gateway, make a GET request to /actuator/gateway/routes. The resulting response is similar to the following:

[{
   "route_id": "first_route",
   "route_object": {
     "predicate": "org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$432/1736826640@1e9d7e7d",
@@ -795,7 +830,7 @@ spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remain
     "filters": []
   },
   "order": 0
-}]

The response contains details of all the routes defined in the gateway. The following table describes the structure of each element (i.e., a route) of the response.

PathTypeDescription

route_id

String

The route id.

route_object.predicate

Object

The route predicate.

route_object.filters

Array

The GatewayFilter factories applied to the route.

order

Number

The route order.

11.5 Retrieving information about a particular route

To retrieve information about a single route, make a GET request to /actuator/gateway/routes/{id} (e.g., /actuator/gateway/routes/first_route). The resulting response is similar to the following:

{
+}]

The response contains details of all the routes defined in the gateway. The following table describes the structure of each element (i.e., a route) of the response.

PathTypeDescription

route_id

String

The route id.

route_object.predicate

Object

The route predicate.

route_object.filters

Array

The GatewayFilter factories applied to the route.

order

Number

The route order.

13.5 Retrieving information about a particular route

To retrieve information about a single route, make a GET request to /actuator/gateway/routes/{id} (e.g., /actuator/gateway/routes/first_route). The resulting response is similar to the following:

{
   "id": "first_route",
   "predicates": [{
     "name": "Path",
@@ -804,12 +839,36 @@ spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remain
   "filters": [],
   "uri": "https://www.uri-destination.org",
   "order": 0
-}]

The following table describes the structure of the response.

PathTypeDescription

id

String

The route id.

predicates

Array

The collection of route predicates. Each item defines the name and the arguments of a given predicate.

filters

Array

The collection of filters applied to the route.

uri

String

The destination URI of the route.

order

Number

The route order.

11.6 Creating and deleting a particular route

To create a route, make a POST request to /gateway/routes/{id_route_to_create} with a JSON body that specifies the fields of the route (see the previous subsection).

To delete a route, make a DELETE request to /gateway/routes/{id_route_to_delete}.

11.7 Recap: list of all endpoints

The table below summarises the Spring Cloud Gateway actuator endpoints. Note that each endpoint has /actuator/gateway as the base-path.

IDHTTP MethodDescription

globalfilters

GET

Displays the list of global filters applied to the routes.

routefilters

GET

Displays the list of GatewayFilter factories applied to a particular route.

refresh

POST

Clears the routes cache.

routes

GET

Displays the list of routes defined in the gateway.

routes/{id}

GET

Displays information about a particular route.

routes/{id}

POST

Add a new route to the gateway.

routes/{id}

DELETE

Remove an existing route from the gateway.

12. Troubleshooting

12.1 Log Levels

Below are some useful loggers that contain valuable trouble shooting infomration at the DEBUG and TRACE levels.

  • org.springframework.cloud.gateway
  • org.springframework.http.server.reactive
  • org.springframework.web.reactive
  • org.springframework.boot.autoconfigure.web
  • reactor.netty
  • redisratelimiter

12.2 Wiretap

The Reactor Netty HttpClient and HttpServer can have wiretap enabled. When combined +}]

The following table describes the structure of the response.

PathTypeDescription

id

String

The route id.

predicates

Array

The collection of route predicates. Each item defines the name and the arguments of a given predicate.

filters

Array

The collection of filters applied to the route.

uri

String

The destination URI of the route.

order

Number

The route order.

13.6 Creating and deleting a particular route

To create a route, make a POST request to /gateway/routes/{id_route_to_create} with a JSON body that specifies the fields of the route (see the previous subsection).

To delete a route, make a DELETE request to /gateway/routes/{id_route_to_delete}.

13.7 Recap: list of all endpoints

The table below summarises the Spring Cloud Gateway actuator endpoints. Note that each endpoint has /actuator/gateway as the base-path.

IDHTTP MethodDescription

globalfilters

GET

Displays the list of global filters applied to the routes.

routefilters

GET

Displays the list of GatewayFilter factories applied to a particular route.

refresh

POST

Clears the routes cache.

routes

GET

Displays the list of routes defined in the gateway.

routes/{id}

GET

Displays information about a particular route.

routes/{id}

POST

Add a new route to the gateway.

routes/{id}

DELETE

Remove an existing route from the gateway.

14. Troubleshooting

14.1 Log Levels

Below are some useful loggers that contain valuable trouble shooting infomration at the DEBUG and TRACE levels.

  • org.springframework.cloud.gateway
  • org.springframework.http.server.reactive
  • org.springframework.web.reactive
  • org.springframework.boot.autoconfigure.web
  • reactor.netty
  • redisratelimiter

14.2 Wiretap

The Reactor Netty HttpClient and HttpServer can have wiretap enabled. When combined with setting the reactor.netty log level to DEBUG or TRACE will enable logging of information such as headers and bodies sent and received across the wire. To enable this, set spring.cloud.gateway.httpserver.wiretap=true and/or spring.cloud.gateway.httpclient.wiretap=true for the HttpServer and HttpClient -respectively.

13. Developer Guide

TODO: overview of writing custom integrations

13.1 Writing Custom Route Predicate Factories

TODO: document writing Custom Route Predicate Factories

13.2 Writing Custom GatewayFilter Factories

In order to write a GatewayFilter you will need to implement GatewayFilterFactory. There is an abstract class called AbstractGatewayFilterFactory which you can extend.

PreGatewayFilterFactory.java.  +respectively.

15. Developer Guide

These are basic guides to writing some custom components of the gateway.

15.1 Writing Custom Route Predicate Factories

In order to write a Route Predicate you will need to implement RoutePredicateFactory. There is an abstract class called AbstractRoutePredicateFactory which you can extend.

MyRoutePredicateFactory.java.  +

public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<HeaderRoutePredicateFactory.Config> {
+
+    public MyRoutePredicateFactory() {
+        super(Config.class);
+    }
+
+    @Override
+    public Predicate<ServerWebExchange> apply(Config config) {
+        // grab configuration from Config object
+        return exchange -> {
+            //grab the request
+            ServerHttpRequest request = exchange.getRequest();
+            //take information from the request to see if it
+            //matches configuration.
+            return matches(config, request);
+        };
+    }
+
+    public static class Config {
+        //Put the configuration properties for your filter here
+    }
+
+}

+

15.2 Writing Custom GatewayFilter Factories

In order to write a GatewayFilter you will need to implement GatewayFilterFactory. There is an abstract class called AbstractGatewayFilterFactory which you can extend.

PreGatewayFilterFactory.java. 

public class PreGatewayFilterFactory extends AbstractGatewayFilterFactory<PreGatewayFilterFactory.Config> {
 
 	public PreGatewayFilterFactory() {
@@ -820,16 +879,16 @@ respectively.

public GatewayFilter apply(Config config) { // grab configuration from Config object return (exchange, chain) -> { - //If you want to build a "pre" filter you need to manipulate the - //request before calling chain.filter - ServerHttpRequest.Builder builder = exchange.getRequest().mutate(); - //use builder to manipulate the request - return chain.filter(exchange.mutate().request(request).build()); + //If you want to build a "pre" filter you need to manipulate the + //request before calling chain.filter + ServerHttpRequest.Builder builder = exchange.getRequest().mutate(); + //use builder to manipulate the request + return chain.filter(exchange.mutate().request(request).build()); }; } public static class Config { - //Put the configuration properties for your filter here + //Put the configuration properties for your filter here } }

@@ -852,11 +911,11 @@ respectively.

public static class Config { - //Put the configuration properties for your filter here + //Put the configuration properties for your filter here } }

-

13.3 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

@Bean
+

15.3 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

@Bean
 public GlobalFilter customGlobalFilter() {
     return (exchange, chain) -> exchange.getPrincipal()
         .map(Principal::getName)
@@ -880,7 +939,7 @@ respectively.

return serverWebExchange; }) .then(); -}

13.4 Writing Custom Route Locators and Writers

TODO: document writing Custom Route Locators and Writers

14. Building a Simple Gateway Using Spring MVC or Webflux

Spring Cloud Gateway provides a utility object called ProxyExchange which you can use inside a regular Spring web handler as a method parameter. It supports basic downstream HTTP exchanges via methods that mirror the HTTP verbs. With MVC it also supports forwarding to a local handler via the forward() method. To use the ProxyExchange just include the right module in your classpath (either spring-cloud-gateway-mvc or spring-cloud-gateway-webflux).

MVC example (proxying a request to "/test" downstream to a remote server):

@RestController
+}

16. Building a Simple Gateway Using Spring MVC or Webflux

[Warning]Warning

The following describes an alternative style gateway. None of the prior documentation applies to what follows.

Spring Cloud Gateway provides a utility object called ProxyExchange which you can use inside a regular Spring web handler as a method parameter. It supports basic downstream HTTP exchanges via methods that mirror the HTTP verbs. With MVC it also supports forwarding to a local handler via the forward() method. To use the ProxyExchange just include the right module in your classpath (either spring-cloud-gateway-mvc or spring-cloud-gateway-webflux).

MVC example (proxying a request to "/test" downstream to a remote server):

@RestController
 @SpringBootApplication
 public class GatewaySampleApplication {
 
diff --git a/2.1.x/spring-cloud-gateway.xml b/2.1.x/spring-cloud-gateway.xml
index fdccf48e..443acc14 100644
--- a/2.1.x/spring-cloud-gateway.xml
+++ b/2.1.x/spring-cloud-gateway.xml
@@ -4,7 +4,7 @@
 
 
 Spring Cloud Gateway
-2020-01-30
+2020-01-31
 
 
 
@@ -59,12 +59,56 @@ working with Spring Cloud Gateway.
 URIs defined in routes without a port will get a default port set to 80 and 443 for HTTP and HTTPS URIs respectively.
 
 
+
+Configuring Route Predicate Factories and Gateway Filter Factories
+There are two ways to configure predicates and filters: shortcuts and fully expanded arguments. Most examples below use the shortcut way.
+The name and argument names will be listed as code in the first sentance or two of the each section. The arguments are typically listed in the order that would be needed for the shortcut configuration.
+
+Shortcut Configuration +Shortcut configuration is recognized by the filter name, followed by an equals sign (=), followed by argument values separated by commas (,). + +application.yml + +spring: + cloud: + gateway: + routes: + - id: after_route + uri: https://example.org + predicates: + - Cookie=mycookie,mycookievalue + + +The previous sample defines the Cookie Route Predicate Factory with two arguments, the cookie name, mycookie and the value to match mycookievalue. +
+
+Fully Expanded Arguments +Fully expanded arguments appear more like standard yaml configuration with name/value pairs. Typically, there will be a name key and an args key. The args key is a map of key value pairs to configure the predicate or filter. + +application.yml + +spring: + cloud: + gateway: + routes: + - id: after_route + uri: https://example.org + predicates: + - name: Cookie + args: + name: mycookie + regexp: mycookievalue + + +This is the full configuration of the shortcut configuration of the Cookie predicate shown above. +
+
Route Predicate Factories Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping infrastructure. Spring Cloud Gateway includes many built-in Route Predicate Factories. All of these predicates match on different attributes of the HTTP request. Multiple Route Predicate Factories can be combined and are combined via logical and.
After Route Predicate Factory -The After Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen after the current datetime. +The After Route Predicate Factory takes one parameter, a datetime (which is a java ZonedDateTime). This predicate matches requests that happen after the current datetime. application.yml @@ -82,7 +126,7 @@ working with Spring Cloud Gateway.
Before Route Predicate Factory -The Before Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen before the current datetime. +The Before Route Predicate Factory takes one parameter, a datetime(which is a java ZonedDateTime). This predicate matches requests that happen before the current datetime. application.yml @@ -100,7 +144,7 @@ working with Spring Cloud Gateway.
Between Route Predicate Factory -The Between Route Predicate Factory takes two parameters, datetime1 and datetime2. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1. +The Between Route Predicate Factory takes two parameters, datetime1 and datetime2 which are java ZonedDateTime objects. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1. application.yml @@ -118,7 +162,7 @@ working with Spring Cloud Gateway.
Cookie Route Predicate Factory -The Cookie Route Predicate Factory takes two parameters, the cookie name and a regular expression. This predicate matches cookies that have the given name and the value matches the regular expression. +The Cookie Route Predicate Factory takes two parameters, the cookie name and a regexp (which is a Java regular expression). This predicate matches cookies that have the given name and the value matches the regular expression. application.yml @@ -136,7 +180,7 @@ working with Spring Cloud Gateway.
Header Route Predicate Factory -The Header Route Predicate Factory takes two parameters, the header name and a regular expression. This predicate matches with a header that has the given name and the value matches the regular expression. +The Header Route Predicate Factory takes two parameters, the header name and a regexp (which is a Java regular expression). This predicate matches with a header that has the given name and the value matches the regular expression. application.yml @@ -154,7 +198,7 @@ working with Spring Cloud Gateway.
Host Route Predicate Factory -The Host Route Predicate Factory takes one parameter: a list of host name patterns. The pattern is an Ant style pattern with . as the separator. This predicates matches the Host header that matches the pattern. +The Host Route Predicate Factory takes one parameter: a list of host name patterns. The pattern is an Ant style pattern with . as the separator. This predicates matches the Host header that matches the pattern. application.yml @@ -174,7 +218,7 @@ working with Spring Cloud Gateway.
Method Route Predicate Factory -The Method Route Predicate Factory takes one or more parameters: the HTTP methods to match. +The Method Route Predicate Factory takes a methods argument which is one or more HTTP methods to match. application.yml @@ -192,7 +236,7 @@ working with Spring Cloud Gateway.
Path Route Predicate Factory -The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher patterns and an optional flag to matchOptionalTrailingSeparator. +The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher patterns and an optional flag to matchOptionalTrailingSeparator. application.yml @@ -215,7 +259,7 @@ String segment = uriVariables.get("segment");
Query Route Predicate Factory -The Query Route Predicate Factory takes two parameters: a required param and an optional regexp. +The Query Route Predicate Factory takes two parameters: a required param and an optional regexp (which is a Java regular expression). application.yml @@ -247,7 +291,7 @@ String segment = uriVariables.get("segment");
RemoteAddr Route Predicate Factory -The RemoteAddr Route Predicate Factory takes a list (min size 1) of CIDR-notation (IPv4 or IPv6) strings, e.g. 192.168.0.1/16 (where 192.168.0.1 is an IP address and 16 is a subnet mask). +The RemoteAddr Route Predicate Factory takes a list (min size 1) of sources, which are CIDR-notation (IPv4 or IPv6) strings, e.g. 192.168.0.1/16 (where 192.168.0.1 is an IP address and 16 is a subnet mask). application.yml @@ -262,6 +306,28 @@ String segment = uriVariables.get("segment"); This route would match if the remote address of the request was, for example, 192.168.1.10. +
+
+Weight Route Predicate Factory +The Weight Route Predicate Factory takes two arguments group and weight (an int). The weights are calculated per group. + +application.yml + +spring: + cloud: + gateway: + routes: + - id: weight_high + uri: https://weighthigh.org + predicates: + - Weight=group1, 8 + - id: weight_low + uri: https://weightlow.org + predicates: + - Weight=group1, 2 + + +This route would forward ~80% of traffic to https://weighthigh.org and ~20% of traffic to https://weighlow.org
Modifying the way remote addresses are resolved By default the RemoteAddr Route Predicate Factory uses the remote address from the incoming request. @@ -334,7 +400,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i NOTE For more detailed examples on how to use any of the following filters, take a look at the unit tests.
AddRequestHeader GatewayFilter Factory -The AddRequestHeader GatewayFilter Factory takes a name and value parameter. +The AddRequestHeader GatewayFilter Factory takes a name and value parameter. application.yml @@ -368,7 +434,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
AddRequestParameter GatewayFilter Factory -The AddRequestParameter GatewayFilter Factory takes a name and value parameter. +The AddRequestParameter GatewayFilter Factory takes a name and value parameter. application.yml @@ -402,7 +468,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
AddResponseHeader GatewayFilter Factory -The AddResponseHeader GatewayFilter Factory takes a name and value parameter. +The AddResponseHeader GatewayFilter Factory takes a name and value parameter. application.yml @@ -436,7 +502,7 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
DedupeResponseHeader GatewayFilter Factory -The DedupeResponseHeader GatewayFilter Factory takes a name parameter and an optional strategy parameter. name can contain a list of header names, space separated. +The DedupeResponseHeader GatewayFilter Factory takes a name parameter and an optional strategy parameter. name can contain a list of header names, space separated. application.yml @@ -456,9 +522,9 @@ If two hops of trusted infrastructure are required before Spring Cloud Gateway i
Hystrix GatewayFilter Factory Hystrix is a library from Netflix that implements the circuit breaker pattern. -The Hystrix GatewayFilter allows you to introduce circuit breakers to your gateway routes, protecting your services from cascading failures and allowing you to provide fallback responses in the event of downstream failures. -To enable Hystrix GatewayFilters in your project, add a dependency on spring-cloud-starter-netflix-hystrix from Spring Cloud Netflix. -The Hystrix GatewayFilter Factory requires a single name parameter, which is the name of the HystrixCommand. +The Hystrix GatewayFilter allows you to introduce circuit breakers to your gateway routes, protecting your services from cascading failures and allowing you to provide fallback responses in the event of downstream failures. +To enable Hystrix GatewayFilters in your project, add a dependency on spring-cloud-starter-netflix-hystrix from Spring Cloud Netflix. +The Hystrix GatewayFilter Factory requires a single name parameter, which is the name of the HystrixCommand. application.yml @@ -588,7 +654,7 @@ their default values:
MapRequestHeader GatewayFilter Factory -The MapRequestHeader GatewayFilter Factory takes 'fromHeader' and 'toHeader' parameters. It creates a new named header (toHeader) and the value is extracted out of an existing named header (fromHeader) from the incoming http request. If the input header does not exist then the filter has no impact. If the new named header already exists then it’s values will be augmented with the new values. +The MapRequestHeader GatewayFilter Facstory takes 'fromHeader' and 'toHeader' parameters. It creates a new named header (toHeader) and the value is extracted out of an existing named header (fromHeader) from the incoming http request. If the input header does not exist then the filter has no impact. If the new named header already exists then it’s values will be augmented with the new values. application.yml @@ -606,7 +672,7 @@ their default values:
PrefixPath GatewayFilter Factory -The PrefixPath GatewayFilter Factory takes a single prefix parameter. +The PrefixPath GatewayFilter Factory takes a single prefix parameter. application.yml @@ -624,7 +690,7 @@ their default values:
PreserveHostHeader GatewayFilter Factory -The PreserveHostHeader GatewayFilter Factory has not parameters. This filter, sets a request attribute that the routing filter will inspect to determine if the original host header should be sent, rather than the host header determined by the http client. +The PreserveHostHeader GatewayFilter Factory has no parameters. This filter, sets a request attribute that the routing filter will inspect to determine if the original host header should be sent, rather than the host header determined by the http client. application.yml @@ -641,7 +707,7 @@ their default values:
RequestRateLimiter GatewayFilter Factory -The RequestRateLimiter GatewayFilter Factory is uses a RateLimiter implementation to determine if the current request is allowed to proceed. If it is not, a status of HTTP 429 - Too Many Requests (by default) is returned. +The RequestRateLimiter GatewayFilter Factory is uses a RateLimiter implementation to determine if the current request is allowed to proceed. If it is not, a status of HTTP 429 - Too Many Requests (by default) is returned. This filter takes an optional keyResolver parameter and parameters specific to the rate limiter (see below). keyResolver is a bean that implements the KeyResolver interface. In configuration, reference the bean by name using SpEL. #{@myKeyResolver} is a SpEL expression referencing a bean with the name myKeyResolver. @@ -719,7 +785,7 @@ KeyResolver userKeyResolver() {
RedirectTo GatewayFilter Factory -The RedirectTo GatewayFilter Factory takes a status and a url parameter. The status should be a 300 series redirect http code, such as 301. The url should be a valid url. This will be the value of the Location header. +The RedirectTo GatewayFilter Factory takes a status and a url parameter. The status should be a 300 series redirect http code, such as 301. The url should be a valid url. This will be the value of the Location header. application.yml @@ -735,41 +801,9 @@ KeyResolver userKeyResolver() { This will send a status 302 with a Location:https://acme.org header to perform a redirect.
-
-RemoveHopByHopHeadersFilter GatewayFilter Factory -The RemoveHopByHopHeadersFilter GatewayFilter Factory removes headers from forwarded requests. The default list of headers that is removed comes from the IETF. - -The default removed headers are: - -Connection - - -Keep-Alive - - -Proxy-Authenticate - - -Proxy-Authorization - - -TE - - -Trailer - - -Transfer-Encoding - - -Upgrade - - -To change this, set the spring.cloud.gateway.filter.remove-non-proxy-headers.headers property to the list of header names to remove. -
RemoveRequestHeader GatewayFilter Factory -The RemoveRequestHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed. +The RemoveRequestHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed. application.yml @@ -787,7 +821,7 @@ KeyResolver userKeyResolver() {
RemoveResponseHeader GatewayFilter Factory -The RemoveResponseHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed. +The RemoveResponseHeader GatewayFilter Factory takes a name parameter. It is the name of the header to be removed. application.yml @@ -808,7 +842,7 @@ and have it applied to all routes.
RewritePath GatewayFilter Factory -The RewritePath GatewayFilter Factory takes a path regexp parameter and a replacement parameter. This uses Java regular expressions for a flexible way to rewrite the request path. +The RewritePath GatewayFilter Factory takes a path regexp parameter and a replacement parameter. This uses Java regular expressions for a flexible way to rewrite the request path. application.yml @@ -828,7 +862,7 @@ and have it applied to all routes.
RewriteLocationResponseHeader GatewayFilter Factory -The RewriteLocationResponseHeader GatewayFilter Factory modifies the value of Location response header, usually to get rid of backend specific details. It takes stripVersionMode, locationHeaderName, hostValue, and protocolsRegex parameters. +The RewriteLocationResponseHeader GatewayFilter Factory modifies the value of Location response header, usually to get rid of backend specific details. It takes stripVersionMode, locationHeaderName, hostValue, and protocolsRegex parameters. application.yml @@ -860,7 +894,7 @@ and have it applied to all routes.
RewriteResponseHeader GatewayFilter Factory -The RewriteResponseHeader GatewayFilter Factory takes name, regexp, and replacement parameters. It uses Java regular expressions for a flexible way to rewrite the response header value. +The RewriteResponseHeader GatewayFilter Factory takes name, regexp, and replacement parameters. It uses Java regular expressions for a flexible way to rewrite the response header value. application.yml @@ -899,7 +933,7 @@ using something like
SecureHeaders GatewayFilter Factory -The SecureHeaders GatewayFilter Factory adds a number of headers to the response at the recommendation from this blog post. +The SecureHeaders GatewayFilter Factory adds a number of headers to the response at the recommendation from this blog post. The following headers are added (along with default values): @@ -993,7 +1027,7 @@ using something like
SetPath GatewayFilter Factory -The SetPath GatewayFilter Factory takes a path template parameter. It offers a simple way to manipulate the request path by allowing templated segments of the path. This uses the uri templates from Spring Framework. Multiple matching segments are allowed. +The SetPath GatewayFilter Factory takes a path template parameter. It offers a simple way to manipulate the request path by allowing templated segments of the path. This uses the uri templates from Spring Framework. Multiple matching segments are allowed. application.yml @@ -1013,7 +1047,7 @@ using something like
SetRequestHeader GatewayFilter Factory -The SetRequestHeader GatewayFilter Factory takes name and value parameters. +The SetRequestHeader GatewayFilter Factory takes name and value parameters. application.yml @@ -1047,7 +1081,7 @@ using something like
SetResponseHeader GatewayFilter Factory -The SetResponseHeader GatewayFilter Factory takes name and value parameters. +The SetResponseHeader GatewayFilter Factory takes name and value parameters. application.yml @@ -1081,7 +1115,7 @@ using something like
SetStatus GatewayFilter Factory -The SetStatus GatewayFilter Factory takes a single status parameter. It must be a valid Spring HttpStatus. It may be the integer value 404 or the string representation of the enumeration NOT_FOUND. +The SetStatus GatewayFilter Factory takes a single status parameter. It must be a valid Spring HttpStatus. It may be the integer value 404 or the string representation of the enumeration NOT_FOUND. application.yml @@ -1123,7 +1157,7 @@ using something like
Retry GatewayFilter Factory -The Retry GatewayFilter Factory support following set of parameters: +The Retry GatewayFilter Factory support following set of parameters: retries: the number of retries that should be attempted @@ -1180,6 +1214,7 @@ If basedOnPreviousValue is true, backoff will be calculated u args: retries: 3 statuses: BAD_GATEWAY + methods: GET,POST backoff: firstBackoff: 10ms maxBackoff: 50ms @@ -1188,15 +1223,15 @@ If basedOnPreviousValue is true, backoff will be calculated u -The retry filter does not currently support retrying with a body (e.g. for POST or PUT requests with a body). - - When using the retry filter with a forward: prefixed URL, the target endpoint should be written carefully so that in case of an error it does not do anything that could result in a response being sent to the client and committed. For example, if the target endpoint is an annotated controller, the target controller method should not return ResponseEntity with an error status code. Instead it should throw an Exception, or signal an error, e.g. via a Mono.error(ex) return value, which the retry filter can be configured to handle by retrying. + +When using the retry filter with any HTTP method with a body, the body will be cached and the gateway will become memory constrained. The body is cached in a request attribute defined by ServerWebExchangeUtils.CACHED_REQUEST_BODY_ATTR. The type of the object is a org.springframework.core.io.buffer.DataBuffer. +
RequestSize GatewayFilter Factory -The RequestSize GatewayFilter Factory can restrict a request from reaching the downstream service , when the request size is greater than the permissible limit. The filter takes RequestSize as parameter which is the permissible size limit of the request defined in bytes. +The RequestSize GatewayFilter Factory can restrict a request from reaching the downstream service , when the request size is greater than the permissible limit. The filter takes a maxSize parameter which is the permissible size limit of the request. The maxSize is a `DataSize type, so values can be defined as a number followed by an optional DataUnit suffix such as 'KB' or 'MB'. The default is 'B' for bytes. application.yml @@ -1223,7 +1258,7 @@ If basedOnPreviousValue is true, backoff will be calculated u
Modify Request Body GatewayFilter Factory This filter is considered BETA and the API may change in the future -This filter can be used to modify the request body before it is sent downstream by the Gateway. +The ModifyRequestBody filter can be used to modify the request body before it is sent downstream by the Gateway. This filter can only be configured using the Java DSL @@ -1258,7 +1293,7 @@ static class Hello {
Modify Response Body GatewayFilter Factory This filter is considered BETA and the API may change in the future -This filter can be used to modify the response body before it is sent back to the Client. +The ModifyResponseBody filter can be used to modify the response body before it is sent back to the Client. This filter can only be configured using the Java DSL @@ -1478,6 +1513,86 @@ or check if an exchange has already been routed.
+ +HttpHeadersFilters +HttpHeadersFilters are applied to requests before sending them downstream, such as in the NettyRoutingFilter. +
+Forwarded Headers Filter +The Forwarded Headers Filter creates a Forwarded header to send to the downstream service. It adds the Host header, scheme and port of the current request to any existing Forwarded header. +
+
+RemoveHopByHop Headers Filter +The RemoveHopByHop Headers Filter removes headers from forwarded requests. The default list of headers that is removed comes from the IETF. + +The default removed headers are: + +Connection + + +Keep-Alive + + +Proxy-Authenticate + + +Proxy-Authorization + + +TE + + +Trailer + + +Transfer-Encoding + + +Upgrade + + +To change this, set the spring.cloud.gateway.filter.remove-non-proxy-headers.headers property to the list of header names to remove. +
+
+XForwarded Headers Filter +The XForwarded Headers Filter creates various a X-Forwarded-* headers to send to the downstream service. It users the Host header, scheme, port and path of the current request to create the various headers. +Creating of individual headers can be controlled by the following boolean properties (defaults to true): + + +spring.cloud.gateway.x-forwarded.for.enabled + + +spring.cloud.gateway.x-forwarded.host.enabled + + +spring.cloud.gateway.x-forwarded.port.enabled + + +spring.cloud.gateway.x-forwarded.proto.enabled + + +spring.cloud.gateway.x-forwarded.prefix.enabled + + +Appending multiple headers can be controlled by the following boolean properties (defaults to true): + + +spring.cloud.gateway.x-forwarded.for.append + + +spring.cloud.gateway.x-forwarded.host.append + + +spring.cloud.gateway.x-forwarded.port.append + + +spring.cloud.gateway.x-forwarded.proto.append + + +spring.cloud.gateway.x-forwarded.prefix.append + + +
+
TLS / SSL The Gateway can listen for requests on https by following the usual Spring server configuration. Example: @@ -1963,10 +2078,38 @@ respectively. Developer Guide -TODO: overview of writing custom integrations +These are basic guides to writing some custom components of the gateway.
Writing Custom Route Predicate Factories -TODO: document writing Custom Route Predicate Factories +In order to write a Route Predicate you will need to implement RoutePredicateFactory. There is an abstract class called AbstractRoutePredicateFactory which you can extend. + +MyRoutePredicateFactory.java + +public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<HeaderRoutePredicateFactory.Config> { + + public MyRoutePredicateFactory() { + super(Config.class); + } + + @Override + public Predicate<ServerWebExchange> apply(Config config) { + // grab configuration from Config object + return exchange -> { + //grab the request + ServerHttpRequest request = exchange.getRequest(); + //take information from the request to see if it + //matches configuration. + return matches(config, request); + }; + } + + public static class Config { + //Put the configuration properties for your filter here + } + +} + +
Writing Custom GatewayFilter Factories @@ -1984,16 +2127,16 @@ respectively. public GatewayFilter apply(Config config) { // grab configuration from Config object return (exchange, chain) -> { - //If you want to build a "pre" filter you need to manipulate the - //request before calling chain.filter - ServerHttpRequest.Builder builder = exchange.getRequest().mutate(); - //use builder to manipulate the request - return chain.filter(exchange.mutate().request(request).build()); + //If you want to build a "pre" filter you need to manipulate the + //request before calling chain.filter + ServerHttpRequest.Builder builder = exchange.getRequest().mutate(); + //use builder to manipulate the request + return chain.filter(exchange.mutate().request(request).build()); }; } public static class Config { - //Put the configuration properties for your filter here + //Put the configuration properties for your filter here } } @@ -2020,7 +2163,7 @@ respectively. } public static class Config { - //Put the configuration properties for your filter here + //Put the configuration properties for your filter here } } @@ -2057,13 +2200,12 @@ public GlobalFilter customGlobalPostFilter() { .then(); }
-
-Writing Custom Route Locators and Writers -TODO: document writing Custom Route Locators and Writers -
Building a Simple Gateway Using Spring MVC or Webflux + +The following describes an alternative style gateway. None of the prior documentation applies to what follows. + Spring Cloud Gateway provides a utility object called ProxyExchange which you can use inside a regular Spring web handler as a method parameter. It supports basic downstream HTTP exchanges via methods that mirror the HTTP verbs. With MVC it also supports forwarding to a local handler via the forward() method. To use the ProxyExchange just include the right module in your classpath (either spring-cloud-gateway-mvc or spring-cloud-gateway-webflux). MVC example (proxying a request to "/test" downstream to a remote server): @RestController