Kaynağa Gözat

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

RuoYi 4 yıl önce
ebeveyn
işleme
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
 import org.springframework.cloud.gateway.filter.GlobalFilter;
7
 import org.springframework.cloud.gateway.filter.GlobalFilter;
8
 import org.springframework.core.Ordered;
8
 import org.springframework.core.Ordered;
9
 import org.springframework.core.io.buffer.DataBuffer;
9
 import org.springframework.core.io.buffer.DataBuffer;
10
+import org.springframework.core.io.buffer.DataBufferFactory;
10
 import org.springframework.core.io.buffer.DataBufferUtils;
11
 import org.springframework.core.io.buffer.DataBufferUtils;
12
+import org.springframework.core.io.buffer.DefaultDataBufferFactory;
11
 import org.springframework.core.io.buffer.NettyDataBufferFactory;
13
 import org.springframework.core.io.buffer.NettyDataBufferFactory;
12
 import org.springframework.http.HttpHeaders;
14
 import org.springframework.http.HttpHeaders;
13
 import org.springframework.http.HttpMethod;
15
 import org.springframework.http.HttpMethod;
@@ -70,10 +72,12 @@ public class XssFilter implements GlobalFilter, Ordered
70
             public Flux<DataBuffer> getBody()
72
             public Flux<DataBuffer> getBody()
71
             {
73
             {
72
                 Flux<DataBuffer> body = super.getBody();
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
                     String bodyStr = new String(content, StandardCharsets.UTF_8);
81
                     String bodyStr = new String(content, StandardCharsets.UTF_8);
78
                     // 防xss攻击过滤
82
                     // 防xss攻击过滤
79
                     bodyStr = EscapeUtil.clean(bodyStr);
83
                     bodyStr = EscapeUtil.clean(bodyStr);