chenjiahe
2022-04-01 5d99e5f0cc856803879ad46844af6139d54244c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.hx.util;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * @author ChenJiaHe
 * @date 2020-12-2
 */
public class IPUtils {
 
    /**获取访问ip地址*/
    public static String getIpAddr(HttpServletRequest request) {
        String ip=request.getHeader("x-forwarded-for");
        if(null!=ip && ip.length()!=0 && !"unknown".equalsIgnoreCase(ip)) {
            if(ip.indexOf(",")!=-1) {
                ip=ip.split(",")[0];
            }
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
            ip=request.getHeader("Proxy-Client-Ip");
        }
        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.getHeader("HTTP_CLIENT_IP");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
            ip=request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
            ip=request.getHeader("X-Real-IP");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
            ip=request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
 
    }
}