package com.mideazy.pay.gateway.filter;
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Charsets;
import com.mideazy.pay.common.component.constant.CommonConstant;
import com.mideazy.pay.gateway.enums.FilterPerformOrderEnum;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
import org.springframework.cloud.gateway.support.BodyInserterContext;
import org.springframework.cloud.gateway.support.DefaultClientResponse;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.*;
import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
/**
* 加密返回参数
* @Author chenjunwen
* @Date 2020/9/29
*/
@Slf4j
@Component
public class GlobalResponseBodyFilter implements GlobalFilter, Ordered {
public static final String APP_PRI_KEY = "appPriKey";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String hxsToken = request.getHeaders().getFirst(CommonConstant.HXS_TOKEN_HEADER_KEY);
if(StrUtil.isNotBlank(hxsToken)){
return chain.filter(exchange);
}
String appPriKey = (String) exchange.getAttributes().get(APP_PRI_KEY);
ServerHttpResponse response = exchange.getResponse();
DataBufferFactory bufferFactory = response.bufferFactory();
ServerHttpResponseDecorator decorator = new ServerHttpResponseDecorator(response) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;//Flux.from(body);
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffers);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
// 释放掉内存
DataBufferUtils.release(join);
String responseData = new String(content, StandardCharsets.UTF_8);
log.info("responseData:{}", responseData);
return bufferFactory.wrap(content);
}));
}
return super.writeWith(body);
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = super.getHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
return chain.filter(exchange.mutate().response(decorator).build());
}
@Override
public int getOrder() {
//WRITE_RESPONSE_FILTER 之前执行,不然不生效
return FilterPerformOrderEnum.PERFORM_ORDER_05.getOrder();
}
}
https://blog.csdn.net/zhaolaisheng123/article/details/103491471
你这问题解决了没?我也是配置之后还是乱码
大哥,你这问题解决了没?
有没有解决 我也遇见这个问题了