From eac52febd59c4a7984f7dd285e99c59facc7e392 Mon Sep 17 00:00:00 2001 From: "review512jwy@163.com" <“review512jwy@163.com”> Date: Wed, 28 Jan 2026 22:35:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E7=9C=9F?= =?UTF-8?q?=E5=AE=9Eip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aspect/OperationLogAspect.java | 3 +- .../dongjian/dashboard/back/util/IPUtils.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 dongjian-dashboard-back-util/src/main/java/com/dongjian/dashboard/back/util/IPUtils.java diff --git a/dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/configurator/aspect/OperationLogAspect.java b/dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/configurator/aspect/OperationLogAspect.java index bfcd2ae..cef1282 100644 --- a/dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/configurator/aspect/OperationLogAspect.java +++ b/dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/configurator/aspect/OperationLogAspect.java @@ -6,6 +6,7 @@ import com.dongjian.dashboard.back.common.config.DataSourceInterceptor; import com.dongjian.dashboard.back.dao.ex.DashboardOperationLogMapperExt; import com.dongjian.dashboard.back.model.DashboardOperationLog; import com.dongjian.dashboard.back.service.common.CommonOpt; +import com.dongjian.dashboard.back.util.IPUtils; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; @@ -69,7 +70,7 @@ public class OperationLogAspect { logEntry.setUri(uri); logEntry.setMethodName(method.getName()); logEntry.setClassName(signature.getDeclaringTypeName()); - logEntry.setIpAddress(request.getRemoteAddr()); + logEntry.setIpAddress(IPUtils.getClientIp(request)); String paramsJson = objectMapper.writeValueAsString(joinPoint.getArgs()); logEntry.setRequestParams(paramsJson); logEntry.setExecutionTimeMs(duration); diff --git a/dongjian-dashboard-back-util/src/main/java/com/dongjian/dashboard/back/util/IPUtils.java b/dongjian-dashboard-back-util/src/main/java/com/dongjian/dashboard/back/util/IPUtils.java new file mode 100644 index 0000000..7716cc8 --- /dev/null +++ b/dongjian-dashboard-back-util/src/main/java/com/dongjian/dashboard/back/util/IPUtils.java @@ -0,0 +1,40 @@ +package com.dongjian.dashboard.back.util; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +@Slf4j +public class IPUtils { + + private IPUtils() {} + + public static String getClientIp(HttpServletRequest request) { + if (request == null) { + return ""; + } + + String ip = request.getHeader("X-Forwarded-For"); + if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { + return ip.split(",")[0].trim(); + } + + ip = request.getHeader("X-Real-IP"); + if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { + return ip; + } + + ip = request.getHeader("Proxy-Client-IP"); + if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { + return ip; + } + + ip = request.getHeader("WL-Proxy-Client-IP"); + if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { + return ip; + } + + return request.getRemoteAddr(); + } +} +