springcloud-gateway-获取入参
在获取入参的时候,get方式的接口可以直接获取到,但是post形式的body获取以后会造成无法再使用,所以必须经过一层转换以后再读取。
直接上代码
@Component
public class CacheBodyGatewayFilter implements GlobalFilter, Ordered {
public static final String CACHE_REQUEST_BODY_OBJECT_KEY = "cachedRequestBodyObject";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
if (exchange.getRequest().getHeaders().getContentType() == null) {
return chain.filter(exchange);
} else {
return DataBufferUtils.join(exchange.getRequest().getBody())
.flatMap(dataBuffer -> {
DataBufferUtils.retain(dataBuffer);
Flux<DataBuffer> cachedFlux = Flux
.defer(() -> Flux.just(dataBuffer.slice(0, dataBuffer.readableByteCount())));
ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(
exchange.getRequest()) {
@Override
public Flux<DataBuffer> getBody() {
return cachedFlux;
}
};
exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, cachedFlux);
return chain.filter(exchange.mutate().request(mutatedRequest).build());
});
}
}
@Override
public int getOrder() {
//这个代表了执行顺序,值越小越先执行
return Ordered.HIGHEST_PRECEDENCE;
}
}
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
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
接下来是读取方法
@Component
public class AccessLogGlobalFilter implements GlobalFilter, Ordered {
public static final String CACHE_REQUEST_BODY_OBJECT_KEY = "cachedRequestBodyObject";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
//Record only http requests (including https)
String schema = request.getURI().getScheme();
if ((!"http".equals(schema) && !"https".equals(schema))) {
return chain.filter(exchange);
}
//我自定义的实体类,如果不需要 你可以直接删掉
LogDTO logDTO = new LogDTO();
logDTO.setPath(request.getURI().getPath());
logDTO.setQueryParams(request.getQueryParams());
logDTO.setMethod(request.getMethod().name());
logDTO.setIp(request.getRemoteAddress().getHostName());
logDTO.setUserAgent(request.getHeaders().getFirst("User-Agent"));
exchange.getAttributes().put("startTime", System.currentTimeMillis());
//获取request body
Flux<DataBuffer> cachedBody = exchange.getAttribute(CACHE_REQUEST_BODY_OBJECT_KEY);
if(cachedBody!=null){
String raw = toRaw(cachedBody);
logDTO.setParams(raw);
}
return returnMono(chain, exchange, logDTO);
}
private Mono<Void> returnMono(GatewayFilterChain chain, ServerWebExchange exchange, LogDTO logDTO) {
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
Long startTime = exchange.getAttribute("startTime");
if (startTime != null) {
long executeTime = (System.currentTimeMillis() - startTime);
logDTO.setTime(executeTime);
logDTO.setResultCode(Objects.requireNonNull(exchange.getResponse().getStatusCode()).value());
StaticLog.info("=================================");
StaticLog.info("请求方式:[{}]", logDTO.getMethod());
StaticLog.info("路径:[{}]", logDTO.getPath());
StaticLog.info("IP:[{}]", logDTO.getIp());
StaticLog.info("UserAgent:[{}]", logDTO.getUserAgent());
StaticLog.info("请求格式[{}]", exchange.getResponse().getHeaders().getContentType());
StaticLog.info("时长:[{}]", executeTime);
StaticLog.info("code:[{}]", logDTO.getResultCode());
StaticLog.info("body:[{}]", logDTO.getParams());
}
}));
}
@Override
public int getOrder() {
//这个filter的执行顺序要在 CacheBodyGatewayFilter 类之后 否则找不到缓存的值
return Ordered.HIGHEST_PRECEDENCE+1;
}
/**
* 用于获取请求参数
*
* @param body
* @return
*/
private static String toRaw(Flux<DataBuffer> body) {
AtomicReference<String> rawRef = new AtomicReference<>();
body.subscribe(buffer -> {
byte[] bytes = new byte[buffer.readableByteCount()];
buffer.read(bytes);
DataBufferUtils.release(buffer);
rawRef.set(Strings.fromUTF8ByteArray(bytes));
});
return rawRef.get();
}
}
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
编辑 (opens new window)
上次更新: 2024-11-06, 19:27:10