|
|
@ -1,8 +1,8 @@ |
|
|
|
/** |
|
|
|
* Copyright (c) 2018 人人开源 All rights reserved. |
|
|
|
* |
|
|
|
* <p> |
|
|
|
* https://www.renren.io
|
|
|
|
* |
|
|
|
* <p> |
|
|
|
* 版权所有,侵权必究! |
|
|
|
*/ |
|
|
|
|
|
|
@ -14,9 +14,7 @@ import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.NetworkInterface; |
|
|
|
import java.net.SocketException; |
|
|
|
import java.util.Enumeration; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
/** |
|
|
|
* IP工具类 |
|
|
@ -26,59 +24,69 @@ import java.util.Enumeration; |
|
|
|
*/ |
|
|
|
public class IpUtils { |
|
|
|
private static Logger logger = LoggerFactory.getLogger(IpUtils.class); |
|
|
|
public static final String LOCALHOST = "127.0.0.1"; |
|
|
|
|
|
|
|
public static final String ANYHOST = "0.0.0.0"; |
|
|
|
|
|
|
|
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); |
|
|
|
|
|
|
|
public static String getIpAddr(HttpServletRequest request) { |
|
|
|
String ip = null; |
|
|
|
try { |
|
|
|
ip = request.getHeader("x-forwarded-for"); |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("Proxy-Client-IP"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("WL-Proxy-Client-IP"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("HTTP_CLIENT_IP"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getRemoteAddr(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("IpUtils ERROR ", e); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getIpAddr(HttpServletRequest request) { |
|
|
|
String ip = null; |
|
|
|
try { |
|
|
|
ip = request.getHeader("x-forwarded-for"); |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("Proxy-Client-IP"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("WL-Proxy-Client-IP"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("HTTP_CLIENT_IP"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR"); |
|
|
|
} |
|
|
|
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { |
|
|
|
ip = request.getRemoteAddr(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("IpUtils ERROR ", e); |
|
|
|
} |
|
|
|
return ip; |
|
|
|
} |
|
|
|
|
|
|
|
return ip; |
|
|
|
} |
|
|
|
/** |
|
|
|
* desc:获取本地Ip |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String getServerIp() { |
|
|
|
String SERVER_IP = ""; |
|
|
|
try { |
|
|
|
/** |
|
|
|
* 遍历本地网卡的方式,在云平台虚拟机取不到真正的ip |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
InetAddress localAddress = InetAddress.getLocalHost(); |
|
|
|
if (isValidAddress(localAddress)) { |
|
|
|
return localAddress.getHostAddress(); |
|
|
|
} |
|
|
|
logger.warn("Could not get local host ip address"); |
|
|
|
return null; |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("IpUtils getServerIp exception ", e); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* desc:获取本地Ip |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String getServerIp(){ |
|
|
|
String SERVER_IP = ""; |
|
|
|
try { |
|
|
|
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); |
|
|
|
InetAddress ip = null; |
|
|
|
while (netInterfaces.hasMoreElements()) { |
|
|
|
NetworkInterface ni = (NetworkInterface) netInterfaces |
|
|
|
.nextElement(); |
|
|
|
ip = ni.getInetAddresses().nextElement(); |
|
|
|
SERVER_IP = ip.getHostAddress(); |
|
|
|
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() |
|
|
|
&& ip.getHostAddress().indexOf(":") == -1) { |
|
|
|
SERVER_IP = ip.getHostAddress(); |
|
|
|
break; |
|
|
|
}else{ |
|
|
|
ip = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (SocketException e) { |
|
|
|
logger.error("IpUtils getServerIp exception ", e); |
|
|
|
} |
|
|
|
return SERVER_IP; |
|
|
|
} |
|
|
|
|
|
|
|
return SERVER_IP; |
|
|
|
} |
|
|
|
private static boolean isValidAddress(InetAddress address) { |
|
|
|
if (address == null || address.isLoopbackAddress()) |
|
|
|
return false; |
|
|
|
String name = address.getHostAddress(); |
|
|
|
return (name != null |
|
|
|
&& !ANYHOST.equals(name) |
|
|
|
&& !LOCALHOST.equals(name) |
|
|
|
&& IP_PATTERN.matcher(name).matches()); |
|
|
|
} |
|
|
|
} |
|
|
|