提交 88352fe0 authored 作者: YunaiV's avatar YunaiV

gateway:优化 AccessLogFilter 访问日志

上级 f4c83c01
package cn.iocoder.yudao.gateway.filter.logging;
import lombok.Data;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.util.MultiValueMap;
import java.util.Date;
import java.util.Map;
/**
* 网关的访问日志
*/
@Data
public class GatewayLog {
/**访问实例*/
private String targetServer;
/**请求路径*/
private String requestPath;
/**请求方法*/
private String requestMethod;
/**协议 */
/**
* 链路追踪编号
*/
private String traceId;
/**
* 用户编号
*/
private Long userId;
/**
* 用户类型
*/
private Integer userType;
/**
* 路由
*
* 类似 ApiAccessLogCreateReqDTO 的 applicationName
*/
private Route route;
/**
* 协议
*/
private String schema;
/**请求体*/
/**
* 请求方法名
*/
private String requestMethod;
/**
* 访问地址
*/
private String requestUrl;
/**
* 查询参数
*/
private MultiValueMap<String, String> queryParams;
/**
* 请求体
*/
private String requestBody;
/**响应体*/
private String responseData;
/**请求ip*/
private String ip;
/**请求时间*/
private Date requestTime;
/**响应时间*/
private Date responseTime;
/**执行时间*/
private long executeTime;
/**
* 请求头
*/
private MultiValueMap<String, String> requestHeaders;
/**
* 用户 IP
*/
private String userIp;
/**
* 响应体
*
* 类似 ApiAccessLogCreateReqDTO 的 resultCode + resultMsg
*/
private String responseBody;
/**
* 响应头
*/
private MultiValueMap<String, String> responseHeaders;
/**
* 开始请求时间
*/
private Date startTime;
/**
* 结束请求时间
*/
private Date endTime;
/**
* 执行时长,单位:毫秒
*/
private Integer duration;
}
package cn.iocoder.yudao.gateway.util;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
......@@ -15,9 +16,6 @@ import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;
/**
* Web 工具类
*
......@@ -71,4 +69,45 @@ public class WebFrameworkUtils {
}));
}
/**
* 获得客户端 IP
*
* 参考 {@link ServletUtil} 的 getClientIP 方法
*
* @param exchange 请求
* @param otherHeaderNames 其它 header 名字的数组
* @return 客户端 IP
*/
public static String getClientIP(ServerWebExchange exchange, String... otherHeaderNames) {
String[] headers = { "X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR" };
if (ArrayUtil.isNotEmpty(otherHeaderNames)) {
headers = ArrayUtil.addAll(headers, otherHeaderNames);
}
// 方式一,通过 header 获取
String ip;
for (String header : headers) {
ip = exchange.getRequest().getHeaders().getFirst(header);
if (!NetUtil.isUnknown(ip)) {
return NetUtil.getMultistageReverseProxyIp(ip);
}
}
// 方式二,通过 remoteAddress 获取
if (exchange.getRequest().getRemoteAddress() == null) {
return null;
}
ip = exchange.getRequest().getRemoteAddress().getHostString();
return NetUtil.getMultistageReverseProxyIp(ip);
}
/**
* 获得请求匹配的 Route 路由
*
* @param exchange 请求
* @return 路由
*/
public static Route getGatewayRoute(ServerWebExchange exchange) {
return exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论