欢迎访问Spring Cloud中国社区

我们致力于成为中国最专业的Spring Boot和Spring Cloud交流社区。推荐使用Github直接登录,欢迎加微信号Software_King进入社区微信交流群。若发现网站bug欢迎反馈!

Spring Cloud Gateway - Audit Log

admin · 26天前 · 245 ·

I wanna to record every request/response into Database(such as MongoDB). Since not find existing audit-like filter, I wrote a simple filter to do that as the following code:

  1. @Component
  2. @Slf4j
  3. @AllArgsConstructor
  4. public class AuditFilter extends AbstractGatewayFilterFactory {
  5. private static final String NOT_APPLICABLE = "[Not applicable]";
  6. private static final String X_REAL_IP = "X-Real-IP";
  7. private static final String X_FORWARDED_FOR = "X-Forwarded-For";
  8. private final AuditProperties properties;
  9. private final ReactiveMongoTemplate mongoOps;
  10. @Override
  11. public GatewayFilter apply(Object config) {
  12. if (!properties.isEnabled()) {
  13. return (exchange, chain) -> chain.filter(exchange);
  14. }
  15. return ((exchange, chain) -> chain.filter(exchange)
  16. .then()
  17. .doOnSubscribe(t -> {
  18. createAuditLog(exchange).subscribe();
  19. }));
  20. }
  21. private Mono<AuditLogPO> createAuditLog(ServerWebExchange exchange) {
  22. log.info("写入MongoDB");
  23. Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
  24. ServerHttpRequest request = exchange.getRequest();
  25. ServerHttpResponse response = exchange.getResponse();
  26. String serviceId = Objects.requireNonNull(route).getId();
  27. AuditLogPO entity = new AuditLogPO();
  28. entity.setServiceId(serviceId);
  29. entity.setStartTimestamp(null);//TODO how to obtain?
  30. entity.setSourceIp(getRealIp(request));
  31. entity.setRequestUri(request.getURI().toString());
  32. entity.setRequestBody(NOT_APPLICABLE);//TODO how to obtain?
  33. if (request.getMethod() != null) {
  34. entity.setRequestMethod(request.getMethod().name());
  35. }
  36. entity.setRequestHeaders(request.getHeaders());
  37. entity.setResponseHeaders(response.getHeaders());
  38. entity.setResponseBody(NOT_APPLICABLE);//TODO how to obtain?
  39. entity.setStatusCode(null == response.getStatusCode() ? HttpStatus.FORBIDDEN.value() : response.getStatusCode().value());
  40. entity.setEndTimestamp(new Date());
  41. return mongoOps.insert(entity);
  42. }
  43. /**
  44. * 获取请求的源IP
  45. */
  46. private String getRealIp(ServerHttpRequest request) {
  47. HttpHeaders headers = request.getHeaders();
  48. String realIp;
  49. if (headers.containsKey(X_REAL_IP)) {
  50. realIp = String.valueOf(headers.getFirst(X_REAL_IP));
  51. } else if (headers.containsKey(X_FORWARDED_FOR)) {
  52. realIp = String.valueOf(headers.getFirst(X_FORWARDED_FOR));
  53. } else {
  54. realIp = request.getRemoteAddress() != null ?
  55. (request.getRemoteAddress().getAddress() != null ?
  56. request.getRemoteAddress().getAddress().getHostAddress() : "") : "";
  57. }
  58. return realIp;
  59. }
  60. }

As you see I have trouble in how to get these data: requestStartTime, requestBody, responseBody. Thanks to any suggestion.

https://stackoverflow.com/questions/51033604/spring-cloud-gateway-audit-log