本文主要研究下spring.cloud.gateway.default-filters

配置

    {
      "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,配置的是FilterDefinition对象

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();
    }
}

从这里可以看到default-filter配置的格式,首先用=分割,取出filter的名称,然后=右边是该filter的参数

实例

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

这里配置了AddResponseHeaderFilter,其参数为X-Response-Default-Name, Default-Value,这里就是header名称和值。

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();
        }
    }
}

它的子类有

  • AddRequestHeaderGatewayFilterFactory
  • AddRequestParameterGatewayFilterFactory
  • AddResponseHeaderGatewayFilterFactory
  • SetRequestHeaderGatewayFilterFactory
  • SetResponseHeaderGatewayFilterFactory

小结

spring.cloud.gateway.default-filters提供了全局的默认过滤器,其配置采用=来分割过滤器的名称及其参数,参数的话,如果是k-v格式的,采用的是NameValueConfig。

doc



作者:go4it
链接:https://www.jianshu.com/p/bb750724865f
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐