Skip to content

Commit c21c7be

Browse files
committed
feat: 添加一个 获取请求 IP 地址的工具方法
1 parent 78d7f77 commit c21c7be

2 files changed

Lines changed: 63 additions & 29 deletions

File tree

simple-common/src/main/java/top/cadecode/common/util/WebUtil.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
package top.cadecode.common.util;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.springframework.http.MediaType;
45
import top.cadecode.common.core.exception.CommonException;
56
import top.cadecode.common.enums.FrameErrorEnum;
67

8+
import javax.servlet.http.HttpServletRequest;
79
import javax.servlet.http.HttpServletResponse;
810
import java.io.IOException;
911
import java.io.PrintWriter;
12+
import java.net.InetAddress;
13+
import java.net.UnknownHostException;
14+
import java.util.Arrays;
15+
import java.util.List;
1016

1117
/**
1218
* @author Cade Li
1319
* @date 2021/12/11
1420
* @description Web 工具类
1521
*/
22+
@Slf4j
1623
public class WebUtil {
1724

25+
/**
26+
* 未知 IP
27+
*/
28+
private static final String UNKNOWN_IP = "unknown";
29+
/**
30+
* 本地 IP 地址
31+
*/
32+
private static final List<String> LOCAL_IP_LIST = Arrays.asList("127.0.0.1", "0:0:0:0:0:0:0:1");
33+
1834
/**
1935
* 向响应对象写入 JSON
2036
*
21-
* @param response 响应对象
37+
* @param response 响应对象
2238
* @param jsonString JSON 字符串
2339
*/
2440
public static void writeJsonToResponse(HttpServletResponse response, String jsonString) {
@@ -33,4 +49,44 @@ public static void writeJsonToResponse(HttpServletResponse response, String json
3349
.suppressed(e);
3450
}
3551
}
52+
53+
/**
54+
* 获取被代理的真实 IP
55+
*
56+
* @param request 请求对象
57+
* @return ip
58+
*/
59+
public static String getIpAddress(HttpServletRequest request) {
60+
// squid
61+
String ip = request.getHeader("X-Forwarded-For");
62+
// tomcat
63+
if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
64+
ip = request.getHeader("Proxy-Client-IP");
65+
}
66+
// webLogic
67+
if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
68+
ip = request.getHeader("WL-Proxy-Client-IP");
69+
}
70+
// nginx
71+
if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
72+
ip = request.getHeader("X-Real-IP");
73+
}
74+
// 从 request 获取
75+
if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
76+
ip = request.getRemoteAddr();
77+
}
78+
// 获取到的 ip 地址可能经过追加,一般使用 ‘,’ 分隔
79+
if (ip.contains(",")) {
80+
ip = ip.split(",")[0];
81+
}
82+
// 本地访问,需要获取本机真正的ip地址
83+
if (LOCAL_IP_LIST.contains(ip)) {
84+
try {
85+
ip = InetAddress.getLocalHost().getHostAddress();
86+
} catch (UnknownHostException e) {
87+
log.error(e.getMessage(), e);
88+
}
89+
}
90+
return ip;
91+
}
3692
}

simple-framework/src/main/java/top/cadecode/framework/aspect/ResponseLoggerAspect.java

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
import org.aspectj.lang.reflect.MethodSignature;
1313
import org.springframework.stereotype.Component;
1414
import org.springframework.util.ObjectUtils;
15-
import org.springframework.util.StringUtils;
1615
import org.springframework.web.context.request.RequestContextHolder;
1716
import org.springframework.web.context.request.ServletRequestAttributes;
1817
import top.cadecode.common.annotation.ResponseLogger;
1918
import top.cadecode.common.util.JsonUtil;
19+
import top.cadecode.common.util.WebUtil;
2020

2121
import javax.servlet.http.HttpServletRequest;
22-
import java.net.InetAddress;
23-
import java.net.UnknownHostException;
24-
import java.util.*;
22+
import java.util.Collections;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import java.util.Objects;
2526

2627
/**
2728
* @author Cade Li
@@ -33,9 +34,6 @@
3334
@Component
3435
public class ResponseLoggerAspect {
3536

36-
// 本地 IP 地址
37-
private static final List<String> LOCAL_IP_LIST = Arrays.asList("127.0.0.1", "0:0:0:0:0:0:0:1");
38-
3937
/**
4038
* 环绕通知
4139
*
@@ -72,7 +70,7 @@ public Object around(ProceedingJoinPoint point, ResponseLogger responseLogger) t
7270
.threadId(Long.toString(Thread.currentThread().getId()))
7371
.threadName(Thread.currentThread().getName())
7472
.classMethod(point.getSignature().getDeclaringTypeName() + '.' + point.getSignature().getName())
75-
.ip(ResponseLoggerAspect.getIpAddress(request))
73+
.ip(WebUtil.getIpAddress(request))
7674
.url(request.getRequestURL().toString())
7775
.httpMethod(request.getMethod())
7876
.requestParams(ResponseLoggerAspect.getRequestParams(point))
@@ -86,26 +84,6 @@ public Object around(ProceedingJoinPoint point, ResponseLogger responseLogger) t
8684
return result;
8785
}
8886

89-
/**
90-
* 获取 IP 地址
91-
*/
92-
public static String getIpAddress(HttpServletRequest request) {
93-
// 解析请求头 X-Forwarded-For 获取主机地址
94-
String ip = request.getHeader("X-Forwarded-For");
95-
if (!StringUtils.hasLength(ip)) {
96-
ip = request.getRemoteAddr();
97-
// 获取本机真正的ip地址
98-
if (LOCAL_IP_LIST.contains(ip)) {
99-
try {
100-
ip = InetAddress.getLocalHost().getHostAddress();
101-
} catch (UnknownHostException e) {
102-
log.error(e.getMessage(), e);
103-
}
104-
}
105-
}
106-
return ip;
107-
}
108-
10987
/**
11088
* 获取方法参数名和参数值
11189
*/

0 commit comments

Comments
 (0)