修复 LogInterceptor获取不到用户真实IP的问题
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.yupi.springbootinit.aop;
|
||||
|
||||
import com.yupi.springbootinit.utils.NetUtils;
|
||||
import java.util.UUID;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -41,9 +42,10 @@ public class LogInterceptor {
|
||||
// 获取请求参数
|
||||
Object[] args = point.getArgs();
|
||||
String reqParam = "[" + StringUtils.join(args, ", ") + "]";
|
||||
String clientIp = NetUtils.getIpAddress(httpServletRequest);
|
||||
// 输出请求日志
|
||||
log.info("request start,id: {}, path: {}, ip: {}, params: {}", requestId, url,
|
||||
httpServletRequest.getRemoteHost(), reqParam);
|
||||
clientIp, reqParam);
|
||||
// 执行原方法
|
||||
Object result = point.proceed();
|
||||
// 输出响应日志
|
||||
@@ -53,4 +55,3 @@ public class LogInterceptor {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.yupi.springbootinit.utils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* 网络工具类
|
||||
@@ -11,6 +13,23 @@ import javax.servlet.http.HttpServletRequest;
|
||||
*/
|
||||
public class NetUtils {
|
||||
|
||||
private static final String UNKNOWN = "unknown";
|
||||
|
||||
private static final String LOCALHOST_IPV4 = "127.0.0.1";
|
||||
|
||||
private static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
|
||||
|
||||
private static final String LOCALHOST_IPV6_SHORT = "::1";
|
||||
|
||||
private static final String[] IP_HEADER_CANDIDATES = {
|
||||
"x-forwarded-for",
|
||||
"X-Forwarded-For",
|
||||
"X-Real-IP",
|
||||
"Proxy-Client-IP",
|
||||
"WL-Proxy-Client-IP",
|
||||
"HTTP_X_FORWARDED_FOR"
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取客户端 IP 地址
|
||||
*
|
||||
@@ -18,38 +37,57 @@ public class NetUtils {
|
||||
* @return
|
||||
*/
|
||||
public static String getIpAddress(HttpServletRequest request) {
|
||||
String ip = request.getHeader("x-forwarded-for");
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
String headerIp = getIpFromHeaders(request);
|
||||
if (StringUtils.isNotBlank(headerIp)) {
|
||||
return headerIp;
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
if (ip.equals("127.0.0.1")) {
|
||||
// 根据网卡取本机配置的 IP
|
||||
InetAddress inet = null;
|
||||
try {
|
||||
inet = InetAddress.getLocalHost();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (inet != null) {
|
||||
ip = inet.getHostAddress();
|
||||
}
|
||||
return normalizeLocalIp(request.getRemoteAddr());
|
||||
}
|
||||
|
||||
private static String getIpFromHeaders(HttpServletRequest request) {
|
||||
for (String headerName : IP_HEADER_CANDIDATES) {
|
||||
String headerValue = request.getHeader(headerName);
|
||||
String ip = extractFirstValidIp(headerValue);
|
||||
if (StringUtils.isNotBlank(ip)) {
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
// 多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
||||
if (ip != null && ip.length() > 15) {
|
||||
if (ip.indexOf(",") > 0) {
|
||||
ip = ip.substring(0, ip.indexOf(","));
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String extractFirstValidIp(String ipValue) {
|
||||
if (StringUtils.isBlank(ipValue)) {
|
||||
return null;
|
||||
}
|
||||
String[] ipList = ipValue.split(",");
|
||||
for (String ip : ipList) {
|
||||
String trimmedIp = StringUtils.trim(ip);
|
||||
if (StringUtils.isNotBlank(trimmedIp) && !UNKNOWN.equalsIgnoreCase(trimmedIp)) {
|
||||
return trimmedIp;
|
||||
}
|
||||
}
|
||||
if (ip == null) {
|
||||
return "127.0.0.1";
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String normalizeLocalIp(String remoteAddr) {
|
||||
if (StringUtils.isBlank(remoteAddr)) {
|
||||
return LOCALHOST_IPV4;
|
||||
}
|
||||
return ip;
|
||||
if (!isLocalhost(remoteAddr)) {
|
||||
return remoteAddr;
|
||||
}
|
||||
try {
|
||||
InetAddress inetAddress = InetAddress.getLocalHost();
|
||||
return inetAddress.getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
return remoteAddr;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isLocalhost(String ip) {
|
||||
return LOCALHOST_IPV4.equals(ip)
|
||||
|| LOCALHOST_IPV6.equals(ip)
|
||||
|| LOCALHOST_IPV6_SHORT.equals(ip);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user