本文主要研究下spring.cloud.gateway.default-filtershtml
{ "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對象java
spring-cloud-gateway-core-2.0.0.RC1-sources.jar!/org/springframework/cloud/gateway/filter/FilterDefinition.javaspring
@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的參數app
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名稱和值。ide
spring-cloud-gateway-core-2.0.0.RC1-sources.jar!/org/springframework/cloud/gateway/filter/factory/AbstractNameValueGatewayFilterFactory.javaui
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(); } } }
它的子類有this
spring.cloud.gateway.default-filters提供了全局的默認過濾器,其配置採用=來分割過濾器的名稱及其參數,參數的話,若是是k-v格式的,採用的是NameValueConfig。code