|
|
package com.example.gateway.filter;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.example.gateway.model.PERMISSION;
|
|
|
import com.example.gateway.model.ResponseBean;
|
|
|
import com.example.gateway.routerImpl.MyServerHttpResponseDecorator;
|
|
|
import com.example.gateway.util.JsonToBean;
|
|
|
import org.reactivestreams.Publisher;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
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.DataBufferFactory;
|
|
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
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.AntPathMatcher;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -42,8 +54,8 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { |
|
|
return exchange.getResponse().setComplete();
|
|
|
}
|
|
|
|
|
|
// ServerHttpResponse response = exchange.getResponse();
|
|
|
// DataBufferFactory bufferFactory = response.bufferFactory();
|
|
|
ServerHttpResponse response = exchange.getResponse();
|
|
|
DataBufferFactory bufferFactory = response.bufferFactory();
|
|
|
// ServerHttpResponseDecorator decorator = new ServerHttpResponseDecorator(response) {
|
|
|
// @Override
|
|
|
// public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
...
|
...
|
@@ -71,8 +83,47 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { |
|
|
// return super.writeWith(body);
|
|
|
// }
|
|
|
// };
|
|
|
// return chain.filter(exchange.mutate().response(decorator).build());
|
|
|
return chain.filter(exchange);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 测试返回中文乱码
|
|
|
*/
|
|
|
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(response) {
|
|
|
@Override
|
|
|
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
|
|
if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux) {
|
|
|
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
|
|
|
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
|
|
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
dataBuffers.forEach(dataBuffer -> {
|
|
|
byte[] content = new byte[dataBuffer.readableByteCount()];
|
|
|
dataBuffer.read(content);
|
|
|
DataBufferUtils.release(dataBuffer);
|
|
|
String tempStr = new String(content, Charset.forName("ISO_8859_1"));
|
|
|
stringBuffer.append(tempStr);
|
|
|
});
|
|
|
|
|
|
String str = new String(stringBuffer);
|
|
|
// String fileKey = "\":\"" + Const.DATA_BASE_FILE_URL_HEAD + Const.M00;
|
|
|
// if (str.contains(fileKey)) {
|
|
|
// //修改
|
|
|
// String value = "\":\"" + Const.SYSTEMS_FILE_URL_HEAD + Const.DATA_BASE_FILE_URL_HEAD + Const.M00;
|
|
|
// str = str.replaceAll(fileKey, value);
|
|
|
// }
|
|
|
|
|
|
byte[] content = str.getBytes(Charset.forName("ISO_8859_1"));
|
|
|
return bufferFactory().wrap(content);
|
|
|
}));
|
|
|
}
|
|
|
// if body is not a flux. never got there.
|
|
|
return super.writeWith(body);
|
|
|
}
|
|
|
};
|
|
|
return chain.filter(exchange.mutate().response(decoratedResponse).build());
|
|
|
//
|
|
|
// return chain.filter(exchange);
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
|