最近在使用SpringCloud Gateway进行网关的开发,自定义过滤器时,需要将ServerHttpRequest中Headers的数据修改后传送给下游服务使用,直接上代码:
1、自定义过滤器

package com.cloudpath.iam.gateway.customerfilter;


import com.cloudpath.iam.gateway.utils.FilterRequestResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author by mazhen
 * @Classname TokenGatewayFilterFactory
 * @Description 用于路由接口的token认证
 * @Date 2020/10/22 10:00
 */
@Component
@Slf4j
public class TokenGatewayFilterFactory extends AbstractGatewayFilterFactory<TokenGatewayFilterFactory.Config> {


    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("enabled");
    }

    public TokenGatewayFilterFactory() {
        super(Config.class);
        log.info("Loaded TokenGatewayFilterFactory");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            if (!config.isEnabled()) {
                return chain.filter(exchange);
            }

            if (null != exchange) {
                ServerHttpRequest httpRequest = exchange.getRequest();

                try {
                    String authUserVo = "将这个数据写入到Headers中";
                    ServerHttpRequest newHttpRequest = FilterRequestResponseUtil.getNewHttpRequest(httpRequest
                            , FilterRequestResponseUtil.getNewHttpHeadersConsumer(authUserVo));
                    return chain.filter(exchange.mutate()
                                .request(newHttpRequest).build());
                } catch (Exception e) {
                    return chain.filter(exchange);
               }
            }
            return chain.filter(exchange);
        };
    }


    public static class Config {
        /**
         * 控制是否开启
         */
        private boolean enabled;

        public Config() {
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
    }
}

2、工具类

package com.cloudpath.iam.gateway.utils;


import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import reactor.core.publisher.Flux;
import java.util.function.Consumer;

/**
 * @author mazhen
 * @className FilterHeadersUtil
 * @Description 过滤器请求/响应工具类
 * @date 2020/10/29 9:31
 */
public final class FilterRequestResponseUtil {

    public static ServerHttpRequest getNewHttpRequest(ServerHttpRequest httpRequest
            , Consumer<HttpHeaders> httpHeadersConsumer, Flux<DataBuffer> dataBufferFlux) {
        ServerHttpRequest newHttpRequest = httpRequest.mutate()
                .headers(httpHeadersConsumer)
                .build();
        return new ServerHttpRequestDecorator(newHttpRequest) {
            @Override
            public Flux<DataBuffer> getBody() {
                return dataBufferFlux;
            }
        };
    }

    public static ServerHttpRequest getNewHttpRequest(ServerHttpRequest httpRequest
            , Consumer<HttpHeaders> httpHeadersConsumer) {
        return  httpRequest.mutate()
                .headers(httpHeadersConsumer)
                .build();
    }

    public static Consumer<HttpHeaders> getNewHttpHeadersConsumer(String accessControlVo) {
        Consumer<HttpHeaders> consumer = headers -> {
            headers.set("data",accessControlVo);
        };
        return consumer;
    }
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐