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.

AddRequestHeadersIfNotPresent

The AddRequestHeadersIfNotPresent filter adds the specified request headers if they are not already present in the original request.

The configuration for the filter accepts a comma-separated list of header names and values, in name:value format:

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: my-gateway-routes
spec:
  service:
    name: myapp
  routes:
    - predicates:
        - Path=/foo
      filters:
        - AddRequestHeadersIfNotPresent=Content-Type:application/json,Connection:keep-alive

In this example, requests to /foo will have the headers Content-Type: application/json and Connection: keep-alive added before they forwarded by the Gateway to the myapp service, if those headers do not exist already in the original request. The values of any headers specified in the filter configuration that do exist in the original request will be left untouched.


ClaimHeader

The ClaimHeader filter can be used to pass a JWT claim value as an HTTP Header. It works both with and without Single Sign-On being activated.

Note: When SSO support is not activated, the ClaimHeader filter will expect the JWT token to be present in the Authorization header, and will not perform token validation itself.

This filter is useful in scenarios where the target service does not handle JWT authorization, but still needs some information from the token.

The configuration for the filter requires 2 parameters:

  1. Claim name: case sensitive name of the JWT claim to pass.
  2. Header name: name of the HTTP header to write the claim value into

The following configuration shows how to extract the sub claim and pass it downstream in an HTTP Header called X-Claim-Sub:

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: myapp-route-config
spec:
  service:
    name: myapp
  routes:
    - predicates:
        - Path=/api/**
      filters:
        - ClaimHeader=sub,X-Claim-Sub

If you need to pass more than one claim, the filter can be applied repeatedly:

filters:
  - ClaimHeader=sub,X-Claim-Sub
  - ClaimHeader=iss,X-Claim-Iss
  - ClaimHeader=iat,X-Claim-Iat

Note In the case where the configured header is already present in the request, the value(s) from the claim will be appended to it. In this way, any existing values sent in the original request are preserved.


JsonToXml

This filter provides a convenient method to convert from JSON to XML, it requires that the Content-Type from the response is application/json and it accepts one attribute to set the root tag for the XML response.

Note Applying transformations on large JSON data could impact response latency.

Note JsonToXml filter can not be used on the same API route where XmlToJson filter is applied.

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: my-gateway-routes
spec:
  service:
    name: myapp
  routes:
  - ssoEnabled: true
    predicates:
      - Path=/json/**
    filters:
      - JsonToXml=custom-response

In the example, if the response contains the following JSON:

{
    "test":  {
       "foo" : "test",
       "bar" : "test"
}

it would become:

<?xml version='1.0' encoding='UTF-8'?>
<custom-response>
    <test>
         <foo>test</foo>
         <bar>test</bar>
    </test>
</custom-response>

Also, if the JSON in the original response doesn't contain any root element and no attribute wrapper is configured, it would add the wrapper response automatically.

Example:

{
  "foo" : "test",
  "bar" : "test"
}

it would become into:

<?xml version='1.0' encoding='UTF-8'?>
<response>
    <foo>test</foo>
    <bar>test</bar>
</response>

RemoveJsonAttributesResponseBody

The RemoveJsonAttributesResponseBody transformation filter can be used to delete attributes from a JSON response body returned from a downstream service, before it is returned by the Gateway to the requesting client.

The configuration for this filter accepts two parameters:

  1. A comma-separated list of attribute names to search for and remove
  2. (Optionally) a boolean value to control whether to remove the attributes from the root level of the body only (false, the default behavior), or recursively (true).

Caution Applying the recursive deletion mode to large JSON response bodies can have a negative impact on service latency.

In the example below, the attributes origin and foo will be deleted from the JSON response body at any level (recursive mode set to true):

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: my-gateway-routes
spec:
  service:
    name: myapp
  routes:
    - predicates:
        - Path=/api/**
      filters:
        - RemoveJsonAttributesResponseBody=origin,foo,true

RewriteAllResponseHeaders

The RewriteAllResponseHeaders filter can be used to apply a transformation to the HTTP headers returned by a downstream service, before the response is returned by the Gateway back to the requesting client.

The filter's configuration accepts a regular expression to search for in header values, and the text to replace any matches with:

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: my-gateway-routes
spec:
  service:
    name: myapp
  routes:
    - predicates:
        - Path=/api/**
      filters:
        - RewriteAllResponseHeaders=\d,0

In this example, any header value containing a number (specified by the single digit matching \d regular expression) will be replaced by 0.


RewriteResponseBody

The RewriteResponseBody filter applies transformations to body content returned by downstream services, before the response is returned by the Gateway back to the requesting client.

The configuration for the filter accepts a comma-separated list of regular expressions to search for in response bodies, and the text to replace any matches with, in regex:replacement format:

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: my-gateway-routes
spec:
  service:
    name: myapp
  routes:
    - predicates:
        - Path=/api/**
      filters:
        - RewriteResponseBody=foo:bar,/path-one/:/path-two/

In this example, in response bodies with content of any type:

  • foo will be replaced by bar
  • /path-one/ will be replaced by /path-two/

RewriteJsonAttributesResponseBody

The RewriteJsonAttributesResponseBody filter applies transformations to JSON response bodies returned by downstream services, before they are returned by the Gateway to the requesting client.

The filter configuration accepts a comma-separated list of transformation instructions. Each instruction is composed of a JSONPath selector expression, followed by the replacement value, in selector:replacement format. For example:

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: my-gateway-routes
spec:
  service:
    name: myapp
  routes:
  - predicates:
      - Path=/api/**
    filters:
      -  RewriteJsonAttributesResponseBody=slides[1].title:Welcome,date:11-11-2022

Given a downstream response containing the following JSON body content:

{
   "date":"01-01-2022 11:00",
   "slides":[
      {
         "title":"Presentation",
         "type":"all"
      },
      {
         "title":"Overview",
         "type":"image"
      }
   ],
   "title":"Sample Title"
}

then applying the example configuration shown above will result in:

  • date at the root level being replaced by 11-11-2022
  • title in the second (index 1) element of the slides array being replaced by Welcome

XmlToJson

This filter provides a convenient method to convert from XML to JSON, it requires that the Content-Type from the response is XML-compliant.

Note Applying transformations on large XML data could impact response latency.

Note XmlToJson filter can not be used on the same API route where JsonToXml filter is applied.

apiVersion: "tanzu.vmware.com/v1"
kind: SpringCloudGatewayRouteConfig
metadata:
  name: my-gateway-routes
spec:
  service:
    name: myapp
  routes:
  - ssoEnabled: true
    predicates:
      - Path=/json/**
    filters:
      - XmlToJson

In the example, if the response contains the following JSON:

<?xml version='1.0' encoding='UTF-8'?>
<test>
     <foo>test</foo>
     <bar>test</bar>
</test>

it would become into:

{
    "test":  {
       "foo" : "test",
       "bar" : "test"
}

Also, if the XML contains any attributes in the original response it would add them as JSON fields.

Example:

<?xml version='1.0' encoding='UTF-8'?>
<test id="1">
    <foo>test</foo>
    <bar>test</bar>
</test>

it would become into:

{
    "test":  {
       "id": "1"
       "foo" : "test",
       "bar" : "test"
}

check-circle-line exclamation-circle-line close-line
Scroll to top icon