File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ ###获取客户端真实的IP地址
2
+
3
+ ``` Java
4
+
5
+ /**
6
+ *
7
+ * @description:获取客户端真实IP
8
+ * @param request
9
+ * @return
10
+ */
11
+ public static String getIpAddr(HttpServletRequest request)
12
+ {
13
+
14
+ if (request == null )
15
+ {
16
+ return " " ;
17
+ }
18
+ String ip = request. getHeader(" x-forwarded-for" );
19
+
20
+ if (ip == null || ip. length() == 0 || " unknown" . equalsIgnoreCase(ip))
21
+ {
22
+ ip = request. getHeader(" Proxy-Client-IP" );
23
+ }
24
+ if (ip == null || ip. length() == 0 || " unknown" . equalsIgnoreCase(ip))
25
+ {
26
+ ip = request. getHeader(" WL-Proxy-Client-IP" );
27
+ }
28
+ if (ip == null || ip. length() == 0 || " unknown" . equalsIgnoreCase(ip))
29
+ {
30
+ ip = request. getRemoteAddr();
31
+ }
32
+ if (ip == null || ip. length() == 0 || " unknown" . equalsIgnoreCase(ip))
33
+ {
34
+ ip = request. getHeader(" http_client_ip" );
35
+ }
36
+ if (ip == null || ip. length() == 0 || " unknown" . equalsIgnoreCase(ip))
37
+ {
38
+ ip = request. getHeader(" HTTP_X_FORWARDED_FOR" );
39
+ }
40
+ // 如果是多级代理,那么取第一个ip为客户ip
41
+ if (ip != null && ip. indexOf(" ," ) != - 1 )
42
+ {
43
+ ip = ip. substring(ip. lastIndexOf(" ," ) + 1 , ip. length()). trim();
44
+ }
45
+ return ip;
46
+ }
47
+ ```
You can’t perform that action at this time.
0 commit comments