The open-source Spring Cloud Gateway project includes a number of built-in filters for use in Gateway routes. The following commercial filters provided by VMware Spring Cloud Gateway for Kubernetes can be used in addition to those included in the OSS project.
These filters are particularly useful when developing custom extensions to Spring Cloud Gateway for Kubernetes, since they can be used to store additional information in the context of incoming requests, making it available for later use in custom filters.
StoreIpAddress
The StoreIpAddress
filter provides a convenient method for storing the IP address of the client making a request coming through the gateway, which can be useful for tracing purposes. The filter configuration accepts a request attribute name under which to store the IP:
apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
name: my-gateway-routes
spec:
service:
name: myapp
filters:
- StoreIpAddress=ipAddress
In this example, the StoreIpAddress
filter stores the IP address in the ipAddress
request attribute of each web exchange. This attribute can then be read when implementing a custom filter, as follows:
((exchange, chain) -> {
String attribute = exchange.getAttributeOrDefault("ipAddress", "Attribute not found");
...
return chain.filter(exchange);
});
StoreHeader
Th StoreHeader
filter provides a convenient method for copying the value of an HTTP header into the attributes of a request coming through the gateway, which can be useful for tracing purposes. The filter configuration accepts a list of header names to search for, and a final parameter with the request attribute name under which to store the header value.
Important The list of header names must be given in priority order. The StoreHeader
filter will look for matching headers in the request in the given order. Only the first header has its value copied into the given request attribute.
apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
name: my-gateway-routes
spec:
service:
name: myapp
filters:
- StoreHeader=x-tracing-header,custom-id,x-custom-id,tracingParam
In this example, the StoreHeader
filter searches requests for x-tracing-header
, custom-id
and x-custom-id
headers. After it finds one (if any are present), it stores that header's value in the tracingParam
attribute of the request. This attribute can then be read when implementing a custom filter, as follows:
((exchange, chain) -> {
String attribute = exchange.getAttributeOrDefault("tracingParam", "Attribute not found");
...
return chain.filter(exchange);
});