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

This filter provides a convenient method to store 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 will store 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

This filter provides a convenient method to copy 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 found will have 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 will search requests for x-tracing-header, custom-id and x-custom-id headers. Once it finds one (if any are present), it will store 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);
});
check-circle-line exclamation-circle-line close-line
Scroll to top icon