import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class AddParamsFilter extends AbstractGatewayFilterFactory<AddParamsFilter.Config> {
public static class Config {
// 定义您的配置属性,如果需要的话
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
// 创建新的参数
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(request.getURI());
builder.queryParam("jwtUser", "jwtUser");
// 更新请求的 URI
ServerHttpRequest newRequest = request.mutate().uri(builder.build().toUri()).build();
// 放行
return chain.filter(exchange.mutate().request(newRequest).build());
};
}
}
请求头加
// 拼接到请求头上
ServerHttpRequest request = exchange.getRequest();
HttpHeaders headers = request.getHeaders();
ServerHttpRequest newRequest = request.mutate().header("jwtUser",""+jwt.get("iss")).build();
ServerHttpRequest newRequest2 = request.mutate().header("jwtBase",""+jwt.get("aud")).build();
return chain.filter(exchange.mutate().request(newRequest).request(newRequest2).build());