【SpringCloud】Gateway 配置全局過濾器獲取請求參數和響應值 實現Ordered介面getOrder()方法,數值越小越靠前執行,記得這一點就OK了。 獲取請求參數RequestBody @Component @Slf4j @AllArgsConstructor public cla ...
【SpringCloud】Gateway 配置全局過濾器獲取請求參數和響應值
實現Ordered介面getOrder()方法,數值越小越靠前執行,記得這一點就OK了。
獲取請求參數RequestBody
@Component
@Slf4j
@AllArgsConstructor
public class HttpRequestFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String method = request.getMethodValue();
String contentType = request.getHeaders().getFirst("Content-Type");
if ("POST".equals(method)) {
return DataBufferUtils.join(exchange.getRequest().getBody())
.flatMap(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
try {
String bodyString = new String(bytes, "utf-8");
log.info(bodyString);//列印請求參數
exchange.getAttributes().put("POST_BODY", bodyString);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
DataBufferUtils.release(dataBuffer);
Flux<DataBuffer> cachedFlux = Flux.defer(() -> {
DataBuffer buffer = exchange.getResponse().bufferFactory()
.wrap(bytes);
return Mono.just(buffer);
});
ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(
exchange.getRequest()) {
@Override
public Flux<DataBuffer> getBody() {
return cachedFlux;
}
};
return chain.filter(exchange.mutate().request(mutatedRequest)
.build());
});
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return -200;
}
}
獲取請求響應值ResponseBody
POSTMAN工具請求里的gzip壓縮頭導致獲取響應值一直亂碼,解決gzip壓縮後響應值獲取
@Slf4j
@Component
public class HttpResponseFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String path = request.getPath().toString();
ServerHttpResponse originalResponse = exchange.getResponse();
System.out.println(originalResponse.isCommitted());
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
return super.writeWith(fluxBody.buffer().map(dataBuffer -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffer);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
//釋放掉記憶體
DataBufferUtils.release(join);
String s = new String(content, StandardCharsets.UTF_8);
List<String> strings = exchange.getResponse().getHeaders().get(HttpHeaders.CONTENT_ENCODING);
if (!CollectionUtils.isEmpty(strings) && strings.contains("gzip")) {
GZIPInputStream gzipInputStream = null;
try {
gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(content), content.length);
StringWriter writer = new StringWriter();
IOUtils.copy(gzipInputStream, writer, "UTF-8");
s = writer.toString();
} catch (IOException e) {
log.error("====Gzip IO error", e);
} finally {
if (gzipInputStream != null) {
try {
gzipInputStream.close();
} catch (IOException e) {
log.error("===Gzip IO close error", e);
}
}
}
} else {
s = new String(content, StandardCharsets.UTF_8);
}
log.info("bodyString: {}", s);//列印請求響應值
return bufferFactory.wrap(content);
}));
}
return super.writeWith(body);
}
};
return chain.filter(exchange.mutate().response(decoratedResponse).build());
}
@Override
public int getOrder() {
return -200;
}
}