背景:
有时要根据不同的请求头或者请求参数动态修改路由的uri
实现方案:
使用全局过滤器(很关键)
https://blog.csdn.net/netyeaxi/article/details/115287385 package com.tl.gateway.filter; import java.net.URI; import java.net.URISyntaxException; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter; import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.core.Ordered; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @Component public class Myf implements GlobalFilter, Ordered { @Override public int getOrder() { return RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER + 1; // return 0; } @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { try { exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, new URI("https://www.baidu.com")); } catch (URISyntaxException e) { e.printStackTrace(); } Mono<Void> ret = chain.filter(exchange); exchange.getResponse().getHeaders(); return ret; } }
标签:uri,springframework,filter,org,过滤器,import,gateway,cloud From: https://www.cnblogs.com/wujf/p/18354709