说明:缓存过滤器一定要在获取数据的过滤器之前执行

1. 创建过滤器缓存数据

package com.lanshen.server.gateway.test;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


@Component
public class CacheBodyGlobalFilter implements Ordered, GlobalFilter {
    public static final String CACHE_REQUEST_BODY_OBJECT_KEY = "cachedRequestBodyObject";
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if (exchange.getRequest().getHeaders().getContentType() == null) {
            return chain.filter(exchange);
        } else {
            return DataBufferUtils.join(exchange.getRequest().getBody())
                    .flatMap(dataBuffer -> {
                        DataBufferUtils.retain(dataBuffer);
                        Flux<DataBuffer> cachedFlux = Flux
                                .defer(() -> Flux.just(dataBuffer.slice(0, dataBuffer.readableByteCount())));
                        ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(
                                exchange.getRequest()) {
                            @Override
                            public Flux<DataBuffer> getBody() {
                                return cachedFlux;
                            }
                        };
                        exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, cachedFlux);
                        return chain.filter(exchange.mutate().request(mutatedRequest).build());
                    });
        }
    }
    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}

2. 获取缓存里面的数据,进行打印

Flux<DataBuffer> body = serverWebExchange.getAttribute("cachedRequestBodyObject");

		body.subscribe(buffer -> {
            byte[] bytes = new byte[buffer.readableByteCount()];
            buffer.read(bytes);
            DataBufferUtils.release(buffer);
            try {
                String bodyString = new String(bytes, "utf-8");
                System.out.println(bodyString);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

3. 格式化数据

	public static Map<String,Object> formdate(String dataValue){
		Map<String, Object> hashMap = new HashMap<>();
		String[] dataValueSplitOne = dataValue.split("----------------------------");
		for (int i = 1; i < dataValueSplitOne.length-1; i++) {
			String dataValueSplitOneValue = dataValueSplitOne[i];
			String[] dataValueSplitTwo = dataValueSplitOneValue.split("name=");
			String[] dataValueSplitThree = dataValueSplitTwo[1].split("\"");
			hashMap.put(dataValueSplitThree[1], dataValueSplitThree[2].trim());
		}
		System.out.println(hashMap);
		return hashMap;
	}

Logo

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

更多推荐