提交 | 用户 | 时间
|
4779be
|
1 |
package cn.stylefeng.guns.modular.zsx.kb.utils; |
懒 |
2 |
|
|
3 |
import javax.servlet.http.HttpServletRequest; |
|
4 |
|
|
5 |
/** |
|
6 |
* @Author: ll |
|
7 |
* @Date: 2021/4/1 9:20 |
|
8 |
* @Version 1.0 |
|
9 |
*/ |
|
10 |
public class IPUtil { |
|
11 |
//获取ip地址 |
|
12 |
public static String getRealIp(HttpServletRequest request) { |
|
13 |
String ip = request.getHeader("x-forwarded-for"); |
|
14 |
System.out.println("x-forwarded-for ip: " + ip); |
|
15 |
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { |
|
16 |
// 多次反向代理后会有多个ip值,第一个ip才是真实ip |
|
17 |
if( ip.indexOf(",")!=-1 ){ |
|
18 |
ip = ip.split(",")[0]; |
|
19 |
} |
|
20 |
} |
|
21 |
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
22 |
ip = request.getHeader("Proxy-Client-IP"); |
|
23 |
} |
|
24 |
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
25 |
ip = request.getHeader("WL-Proxy-Client-IP"); |
|
26 |
} |
|
27 |
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
28 |
ip = request.getHeader("HTTP_CLIENT_IP"); |
|
29 |
} |
|
30 |
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
31 |
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
|
32 |
} |
|
33 |
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
34 |
ip = request.getHeader("X-Real-IP"); |
|
35 |
} |
|
36 |
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
37 |
ip = request.getRemoteAddr(); |
|
38 |
} |
|
39 |
return ip; |
|
40 |
} |
|
41 |
} |