正在显示
1 个修改的文件
包含
55 行增加
和
4 行删除
1 | package com.example.gateway.filter; | 1 | package com.example.gateway.filter; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.example.gateway.model.PERMISSION; | 4 | import com.example.gateway.model.PERMISSION; |
5 | +import com.example.gateway.model.ResponseBean; | ||
6 | +import com.example.gateway.routerImpl.MyServerHttpResponseDecorator; | ||
4 | import com.example.gateway.util.JsonToBean; | 7 | import com.example.gateway.util.JsonToBean; |
8 | +import org.reactivestreams.Publisher; | ||
5 | import org.springframework.beans.factory.annotation.Autowired; | 9 | import org.springframework.beans.factory.annotation.Autowired; |
6 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; | 10 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
7 | import org.springframework.cloud.gateway.filter.GlobalFilter; | 11 | import org.springframework.cloud.gateway.filter.GlobalFilter; |
8 | import org.springframework.core.Ordered; | 12 | import org.springframework.core.Ordered; |
13 | +import org.springframework.core.io.buffer.DataBuffer; | ||
14 | +import org.springframework.core.io.buffer.DataBufferFactory; | ||
15 | +import org.springframework.core.io.buffer.DataBufferUtils; | ||
9 | import org.springframework.data.redis.core.StringRedisTemplate; | 16 | import org.springframework.data.redis.core.StringRedisTemplate; |
10 | import org.springframework.http.HttpStatus; | 17 | import org.springframework.http.HttpStatus; |
11 | import org.springframework.http.server.reactive.ServerHttpRequest; | 18 | import org.springframework.http.server.reactive.ServerHttpRequest; |
19 | +import org.springframework.http.server.reactive.ServerHttpResponse; | ||
20 | +import org.springframework.http.server.reactive.ServerHttpResponseDecorator; | ||
12 | import org.springframework.stereotype.Component; | 21 | import org.springframework.stereotype.Component; |
13 | import org.springframework.util.AntPathMatcher; | 22 | import org.springframework.util.AntPathMatcher; |
14 | import org.springframework.web.server.ServerWebExchange; | 23 | import org.springframework.web.server.ServerWebExchange; |
24 | +import reactor.core.publisher.Flux; | ||
15 | import reactor.core.publisher.Mono; | 25 | import reactor.core.publisher.Mono; |
16 | 26 | ||
27 | +import java.nio.charset.Charset; | ||
28 | +import java.util.ArrayList; | ||
17 | import java.util.List; | 29 | import java.util.List; |
18 | 30 | ||
19 | /** | 31 | /** |
@@ -42,8 +54,8 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -42,8 +54,8 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
42 | return exchange.getResponse().setComplete(); | 54 | return exchange.getResponse().setComplete(); |
43 | } | 55 | } |
44 | 56 | ||
45 | -// ServerHttpResponse response = exchange.getResponse(); | ||
46 | -// DataBufferFactory bufferFactory = response.bufferFactory(); | 57 | + ServerHttpResponse response = exchange.getResponse(); |
58 | + DataBufferFactory bufferFactory = response.bufferFactory(); | ||
47 | // ServerHttpResponseDecorator decorator = new ServerHttpResponseDecorator(response) { | 59 | // ServerHttpResponseDecorator decorator = new ServerHttpResponseDecorator(response) { |
48 | // @Override | 60 | // @Override |
49 | // public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { | 61 | // public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { |
@@ -71,8 +83,47 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -71,8 +83,47 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
71 | // return super.writeWith(body); | 83 | // return super.writeWith(body); |
72 | // } | 84 | // } |
73 | // }; | 85 | // }; |
74 | -// return chain.filter(exchange.mutate().response(decorator).build()); | ||
75 | - return chain.filter(exchange); | 86 | + |
87 | + | ||
88 | + | ||
89 | + /** | ||
90 | + * 测试返回中文乱码 | ||
91 | + */ | ||
92 | + ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(response) { | ||
93 | + @Override | ||
94 | + public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { | ||
95 | + if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux) { | ||
96 | + Flux<? extends DataBuffer> fluxBody = Flux.from(body); | ||
97 | + return super.writeWith(fluxBody.buffer().map(dataBuffers -> { | ||
98 | + | ||
99 | + StringBuffer stringBuffer = new StringBuffer(); | ||
100 | + dataBuffers.forEach(dataBuffer -> { | ||
101 | + byte[] content = new byte[dataBuffer.readableByteCount()]; | ||
102 | + dataBuffer.read(content); | ||
103 | + DataBufferUtils.release(dataBuffer); | ||
104 | + String tempStr = new String(content, Charset.forName("ISO_8859_1")); | ||
105 | + stringBuffer.append(tempStr); | ||
106 | + }); | ||
107 | + | ||
108 | + String str = new String(stringBuffer); | ||
109 | +// String fileKey = "\":\"" + Const.DATA_BASE_FILE_URL_HEAD + Const.M00; | ||
110 | +// if (str.contains(fileKey)) { | ||
111 | +// //修改 | ||
112 | +// String value = "\":\"" + Const.SYSTEMS_FILE_URL_HEAD + Const.DATA_BASE_FILE_URL_HEAD + Const.M00; | ||
113 | +// str = str.replaceAll(fileKey, value); | ||
114 | +// } | ||
115 | + | ||
116 | + byte[] content = str.getBytes(Charset.forName("ISO_8859_1")); | ||
117 | + return bufferFactory().wrap(content); | ||
118 | + })); | ||
119 | + } | ||
120 | + // if body is not a flux. never got there. | ||
121 | + return super.writeWith(body); | ||
122 | + } | ||
123 | + }; | ||
124 | + return chain.filter(exchange.mutate().response(decoratedResponse).build()); | ||
125 | +// | ||
126 | +// return chain.filter(exchange); | ||
76 | } | 127 | } |
77 | 128 | ||
78 | @Override | 129 | @Override |
-
请 注册 或 登录 后发表评论