-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharsetConversionFilter.java
More file actions
48 lines (40 loc) · 1.94 KB
/
CharsetConversionFilter.java
File metadata and controls
48 lines (40 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
@Component
public class CharsetConversionFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
// 检查请求头中的字符集
HttpHeaders headers = request.getHeaders();
if (headers.getContentType() != null && "application/json".equals(headers.getContentType().toString())) {
String charset = headers.getContentType().getCharSet().name();
if ("GBK".equals(charset)) {
// 转换请求体的字符集
byte[] body = request.getBodyAsBytes().block();
String decodedBody = new String(body, StandardCharsets.GBK);
byte[] encodedBody = decodedBody.getBytes(StandardCharsets.UTF_8);
// 创建一个新的请求对象,包含转换后的请求体
ServerHttpRequest newRequest = request.mutate()
.body(Mono.just(encodedBody))
.build();
// 使用新的请求对象继续处理链
return chain.filter(exchange.mutate().request(newRequest).build());
}
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0; // 确定过滤器的执行顺序
}
}