|
@@ -6,6 +6,7 @@ import com.fdkankan.gateway.log.GatewayLog;
|
|
|
import com.sun.deploy.util.StringUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.reactivestreams.Publisher;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
|
|
@@ -17,6 +18,7 @@ import org.springframework.core.io.buffer.DataBuffer;
|
|
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
|
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
|
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
@@ -46,13 +48,15 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
private final List<HttpMessageReader<?>> messageReaders = HandlerStrategies.withDefaults().messageReaders();
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
+
|
|
|
@Override
|
|
|
public int getOrder() {
|
|
|
return -100;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
|
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
@@ -101,7 +105,6 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 解决 request body 只能读取一次问题,
|
|
|
* 参考: org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory
|
|
|
* @param exchange
|
|
|
* @param chain
|
|
@@ -147,12 +150,12 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
/**
|
|
|
* 打印日志
|
|
|
- * @author javadaily
|
|
|
- * @date 2021/3/24 14:53
|
|
|
* @param gatewayLog 网关日志
|
|
|
*/
|
|
|
private void writeAccessLog(GatewayLog gatewayLog) {
|
|
|
log.info(gatewayLog.toString());
|
|
|
+ //日志写入mongodb
|
|
|
+ mongoTemplate.insert(gatewayLog, "gatewayLog");
|
|
|
}
|
|
|
|
|
|
|
|
@@ -180,8 +183,6 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
if (contentLength > 0) {
|
|
|
httpHeaders.setContentLength(contentLength);
|
|
|
} else {
|
|
|
- // TODO: this causes a 'HTTP/1.1 411 Length Required' // on
|
|
|
- // httpbin.org
|
|
|
httpHeaders.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
|
|
|
}
|
|
|
return httpHeaders;
|