package com.boying.util;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.net.InetAddress;
|
import java.net.UnknownHostException;
|
|
public class IpUtil {
|
public static String getIpAddr1(HttpServletRequest request) {
|
String ipAddress = null;
|
try {
|
ipAddress = request.getHeader("x-forwarded-for");
|
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
ipAddress = request.getHeader("Proxy-Client-IP");
|
}
|
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
ipAddress = request.getRemoteAddr();
|
if (ipAddress.equals("127.0.0.1")) {
|
// 根据网卡取本机配置的IP
|
InetAddress inet = null;
|
try {
|
inet = InetAddress.getLocalHost();
|
} catch (UnknownHostException e) {
|
e.printStackTrace();
|
}
|
ipAddress = inet.getHostAddress();
|
}
|
}
|
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
|
// = 15
|
if (ipAddress.indexOf(",") > 0) {
|
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
}
|
}
|
} catch (Exception e) {
|
ipAddress = "";
|
}
|
// ipAddress = this.getRequest().getRemoteAddr();
|
|
return "0:0:0:0:0:0:0:1".equals(ipAddress) ? "127.0.0.1" : ipAddress;
|
}
|
|
/**
|
* 获取真实ip地址,不返回内网地址
|
*
|
* @param request
|
* @return
|
*/
|
public static String getIpAddr2(HttpServletRequest request) {
|
//目前则是网关ip
|
String ip = request.getHeader("X-Real-IP");
|
if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
}
|
ip = request.getHeader("X-Forwarded-For");
|
if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
int index = ip.indexOf(',');
|
if (index != -1) {
|
//只获取第一个值
|
return ip.substring(0, index);
|
} else {
|
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
}
|
} else {
|
//取不到真实ip则返回空,不能返回内网地址。
|
return "";
|
}
|
}
|
|
|
//其中ip的获取方式
|
public static String getIpAddr(HttpServletRequest request) {
|
|
String ip = request.getHeader("x-forwarded-for");
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("X-Real-IP");
|
//LOGGER.error("X-Real-IP:"+ip);
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("http_client_ip");
|
// LOGGER.error("http_client_ip:"+ip);
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
// LOGGER.error("getRemoteAddr:"+ip);
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
// LOGGER.error("Proxy-Client-IP:"+ip);
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
// LOGGER.error("WL-Proxy-Client-IP:"+ip);
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
// LOGGER.error("HTTP_X_FORWARDED_FOR:"+ip);
|
}
|
// 如果是多级代理,那么取第一个ip为客户ip
|
if (ip != null && ip.indexOf(",") != -1) {
|
ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
|
// LOGGER.error("ip:"+ip);
|
}
|
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
}
|
|
}
|