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