MCPcopy Create free account
hub / github.com/Exrick/xpay / IpInfoUtils

Class IpInfoUtils

xpay-code/src/main/java/cn/exrick/common/utils/IpInfoUtils.java:14–52  ·  view source on GitHub ↗

@author Exrickx

Source from the content-addressed store, hash-verified

12 * @author Exrickx
13 */
14public class IpInfoUtils {
15
16 private static final Logger log = LoggerFactory.getLogger(IpInfoUtils.class);
17
18 /**
19 * 获取客户端IP地址
20 * @param request 请求
21 * @return
22 */
23 public static String getIpAddr(HttpServletRequest request) {
24 String ip = request.getHeader("x-forwarded-for");
25 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
26 ip = request.getHeader("Proxy-Client-IP");
27 }
28 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
29 ip = request.getHeader("WL-Proxy-Client-IP");
30 }
31 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
32 ip = request.getRemoteAddr();
33 if (ip.equals("127.0.0.1")) {
34 //根据网卡取本机配置的IP
35 InetAddress inet = null;
36 try {
37 inet = InetAddress.getLocalHost();
38 } catch (UnknownHostException e) {
39 e.printStackTrace();
40 }
41 ip = inet.getHostAddress();
42 }
43 }
44 // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
45 if (ip != null && ip.length() > 15) {
46 if (ip.indexOf(",") > 0) {
47 ip = ip.substring(0, ip.indexOf(","));
48 }
49 }
50 return ip;
51 }
52}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected