Talk about spring.cloud.gateway.default-filters

  springcloud

Order

This article mainly studies spring.cloud.gateway.default-filters

Configuration

    {
      "sourceType": "org.springframework.cloud.gateway.config.GatewayProperties",
      "name": "spring.cloud.gateway.default-filters",
      "description": "List of filter definitions that are applied to every route.",
      "type": "java.util.List<org.springframework.cloud.gateway.filter.FilterDefinition>"
    }

Default-filters, configured with FilterDefinition object

FilterDefinition

spring-cloud-gateway-core-2.0.0.RC1-sources.jar! /org/springframework/cloud/gateway/filter/FilterDefinition.java

@Validated
public class FilterDefinition {
    @NotNull
    private String name;
    private Map<String, String> args = new LinkedHashMap<>();

    public FilterDefinition() {
    }

    public FilterDefinition(String text) {
        int eqIdx = text.indexOf("=");
        if (eqIdx <= 0) {
            setName(text);
            return;
        }
        setName(text.substring(0, eqIdx));

        String[] args = tokenizeToStringArray(text.substring(eqIdx+1), ",");

        for (int i=0; i < args.length; i++) {
            this.args.put(NameUtils.generateName(i), args[i]);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, String> getArgs() {
        return args;
    }

    public void setArgs(Map<String, String> args) {
        this.args = args;
    }

    public void addArg(String key, String value) {
        this.args.put(key, value);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FilterDefinition that = (FilterDefinition) o;
        return Objects.equals(name, that.name) &&
                Objects.equals(args, that.args);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, args);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("FilterDefinition{");
        sb.append("name='").append(name).append('\'');
        sb.append(", args=").append(args);
        sb.append('}');
        return sb.toString();
    }
}

From here, you can see the format of the default-filter configuration. First use = to divide, take out the name of the filter, and then = to the right is the parameter of the filter.

Example

spring:
  cloud:
    gateway:
      default-filters:
      - AddResponseHeader=X-Response-Default-Name, Default-Value
      - AddRequestHeader=X-Request-Name, Value
      - AddRequestParameter=name, value

AddResponseHeaderFilter is configured here, and its parameters are x-response-default-name, default-value, here is the header name and value.

AbstractNameValueGatewayFilterFactory

spring-cloud-gateway-core-2.0.0.RC1-sources.jar! /org/springframework/cloud/gateway/filter/factory/AbstractNameValueGatewayFilterFactory.java

public abstract class AbstractNameValueGatewayFilterFactory extends AbstractGatewayFilterFactory<AbstractNameValueGatewayFilterFactory.NameValueConfig> {

    public AbstractNameValueGatewayFilterFactory() {
        super(NameValueConfig.class);
    }

    public List<String> shortcutFieldOrder() {
        return Arrays.asList(GatewayFilter.NAME_KEY, GatewayFilter.VALUE_KEY);
    }


    @Validated
    public static class NameValueConfig {
        @NotEmpty
        protected String name;
        @NotEmpty
        protected String value;

        public String getName() {
            return name;
        }

        public NameValueConfig setName(String name) {
            this.name = name;
            return this;
        }

        public String getValue() {
            return value;
        }

        public NameValueConfig setValue(String value) {
            this.value = value;
            return this;
        }

        @Override
        public String toString() {
            return new ToStringCreator(this)
                    .append("name", name)
                    .append("value", value)
                    .toString();
        }
    }
}

Its subclasses are

  • AddRequestHeaderGatewayFilterFactory
  • AddRequestParameterGatewayFilterFactory
  • AddResponseHeaderGatewayFilterFactory
  • SetRequestHeaderGatewayFilterFactory
  • SetResponseHeaderGatewayFilterFactory

Summary

Spring. Cloud. Gateway. Default-Filters provides a global default filter. Its configuration uses = to split the filter name and its parameters. If the parameters are in k-v format, it uses NameValueConfig.

doc