Browse Source

修复响应体过大出现的乱码问题

RuoYi 4 years ago
parent
commit
360ccc7adc

+ 8 - 4
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/XssFilter.java

@@ -7,7 +7,9 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
7 7
 import org.springframework.cloud.gateway.filter.GlobalFilter;
8 8
 import org.springframework.core.Ordered;
9 9
 import org.springframework.core.io.buffer.DataBuffer;
10
+import org.springframework.core.io.buffer.DataBufferFactory;
10 11
 import org.springframework.core.io.buffer.DataBufferUtils;
12
+import org.springframework.core.io.buffer.DefaultDataBufferFactory;
11 13
 import org.springframework.core.io.buffer.NettyDataBufferFactory;
12 14
 import org.springframework.http.HttpHeaders;
13 15
 import org.springframework.http.HttpMethod;
@@ -70,10 +72,12 @@ public class XssFilter implements GlobalFilter, Ordered
70 72
             public Flux<DataBuffer> getBody()
71 73
             {
72 74
                 Flux<DataBuffer> body = super.getBody();
73
-                return body.map(dataBuffer -> {
74
-                    byte[] content = new byte[dataBuffer.readableByteCount()];
75
-                    dataBuffer.read(content);
76
-                    DataBufferUtils.release(dataBuffer);
75
+                return body.buffer().map(dataBuffers -> {
76
+                    DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
77
+                    DataBuffer join = dataBufferFactory.join(dataBuffers);
78
+                    byte[] content = new byte[join.readableByteCount()];
79
+                    join.read(content);
80
+                    DataBufferUtils.release(join);
77 81
                     String bodyStr = new String(content, StandardCharsets.UTF_8);
78 82
                     // 防xss攻击过滤
79 83
                     bodyStr = EscapeUtil.clean(bodyStr);