文档章节

聊聊spring.cloud.gateway.default-filters

go4it
 go4it
发布于 06/01 22:48
字数 420
阅读 67
收藏 1

本文主要研究下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
粉丝 51
博文 700
码字总数 501910
作品 0
深圳
聊聊spring cloud gateway的GatewayFilter

序 本文主要研究一下spring cloud gateway的GatewayFilter GatewayFilter spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/filter/GatewayFilter.jav......

go4it
06/09
0
0
聊聊spring cloud gateway的GlobalFilter

序 本文主要研究一下spring cloud gateway的GlobalFilter GatewayAutoConfiguration spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/config/Gateway......

go4it
06/08
0
0
聊聊spring cloud的DiscoveryClientRouteDefinitionLocator

序 本文主要研究一下spring cloud的DiscoveryClientRouteDefinitionLocator GatewayAutoConfiguration spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gatew......

go4it
06/05
0
0
聊聊spring cloud gateway的RouteLocator

序 本文主要研究一下spring cloud gateway的RouteLocator RouteLocator spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/route/RouteLocator.java 其有......

go4it
06/06
0
0
聊聊spring cloud gateway的SetStatusGatewayFilter

序 本文主要研究下spring cloud gateway的SetStatusGatewayFilter GatewayAutoConfiguration spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/config......

go4it
06/15
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

HTTPS is easy

HTTPS is easy https://www.troyhunt.com/https-is-easy/ HTTPS is easy! In fact, it's so easy I decided to create 4 short videos around 5 minutes each to show people how to enable ......

openthings
28分钟前
0
0
bugList 2

用户端: 1. 上传文件时,当选择:彩色-A3-双面时,第二个图片有bug 应改为 和第一个图片的类型相同 2. 确认打印时,三个下拉选目前有bug 应改为:根据后台配置的商家,group by计算出不同城...

勇恒
31分钟前
2
0
keras cnn 网咯 mnist 分类

搭建貌似比tf是简单很多。。。。。 from keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.models import Sequentialfrom keras.layers import Dense, Activat......

阿豪boy
33分钟前
0
0
解决 /var/run/nginx.pid failed

nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory) sudo nginx -c /etc/nginx/nginx.conf nginx -s reload...

驛路梨花醉美
35分钟前
0
0
nginx负载均衡-ssl原理-生成ssl密钥对-nginx配置ssl

nginx负载均衡: 1.创建配置文件 vim /usr/local/nginx/conf/vhost/load.conf #添加以下内容: upstream qq_com #名字自定义,借助此模块定义多个IP,后面...

ZHENG-JY
35分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部