spring-cloud-gateway 路由 Route 常见的 11 个 Attributes

package cn.com.xuxiaowei.artifact.gateway.filter;

import org.jspecify.annotations.NullMarked;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.observation.ServerRequestObservationContext;
import org.springframework.web.accept.ApiVersionHolder;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import reactor.util.context.Context;

import java.util.Map;

public class TestGatewayFilterFactory extends AbstractGatewayFilterFactory<TestGatewayFilterFactory.Config>
		implements Ordered {

	private int order = -10;

	@Override
	public int getOrder() {
		return order;
	}

	@NullMarked
	@Override
	public GatewayFilter apply(Config config) {
		GatewayFilter filter = (exchange, chain) -> {
			Map<String, Object> attributes = exchange.getAttributes();

			String gatewayPredicateMatchedPathAttr = (String) attributes.get(ServerWebExchangeUtils.GATEWAY_PREDICATE_MATCHED_PATH_ATTR);
			Map uriTemplateVariables = (Map) attributes.get(ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
			PathContainer gatewayPredicatePathContainer = (PathContainer) attributes.get(ServerWebExchangeUtils.GATEWAY_PREDICATE_PATH_CONTAINER_ATTR);
			Map routeWeight = (Map) attributes.get(ServerWebExchangeUtils.WEIGHT_ATTR);
			Context gatewayReactorContext = (Context) attributes.get(ServerWebExchangeUtils.GATEWAY_REACTOR_CONTEXT_ATTR);
			ApiVersionHolder apiVersion = (ApiVersionHolder) attributes.get(HandlerMapping.API_VERSION_ATTRIBUTE);
			String gatewayHandlerMapper = (String) attributes.get(ServerWebExchangeUtils.GATEWAY_HANDLER_MAPPER_ATTR);
			String logId = (String) attributes.get(ServerWebExchange.LOG_ID_ATTRIBUTE);
			ServerRequestObservationContext serverRequestObservationContext = (ServerRequestObservationContext) attributes.get(ServerRequestObservationContext.CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE);
			Route gatewayRoute = (Route) attributes.get(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
			String gatewayPredicateMatchedPathRouteIdAttr = (String) attributes.get(ServerWebExchangeUtils.GATEWAY_PREDICATE_MATCHED_PATH_ROUTE_ID_ATTR);

			return chain.filter(exchange);
		};

		return new OrderedGatewayFilter(filter, order);
	}

	public static class Config {

	}

}