记得上下班打卡 | git大法好,push需谨慎

Commit 283823be authored by 张国柄's avatar 张国柄

fix get client IP util;

parent 91c35d90
......@@ -3,6 +3,8 @@ package com.liquidnet.commons.lang.util;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
......@@ -14,6 +16,10 @@ public class CurrentUtil {
public static final String CLI_VERSION = "version";
public static final String CLI_UNKNOWN = "unknown";
private static final String IP_UTILS_FLAG = ",";
private static final String LOCALHOST_IP = "0:0:0:0:0:0:0:1";
private static final String LOCALHOST_IP1 = "127.0.0.1";
public static String getToken() {
String authToken = ServletUtils.getRequest().getHeader(uToken);
return StringUtils.isEmpty(authToken) ? "" : authToken;
......@@ -55,6 +61,20 @@ public class CurrentUtil {
}
if (ip == null || ip.length() == 0 || CLI_UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (LOCALHOST_IP1.equalsIgnoreCase(ip) || LOCALHOST_IP.equalsIgnoreCase(ip)) {
// 根据网卡取本机配置的IP
InetAddress iNet = null;
try {
iNet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ip = iNet.getHostAddress();
}
}
// 使用代理,则获取第一个IP地址
if (!StringUtils.isEmpty(ip) && ip.indexOf(IP_UTILS_FLAG) > 0) {
ip = ip.substring(0, ip.indexOf(IP_UTILS_FLAG));
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment