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