1、获取IP地址
注意:IP地址经过多次反向代理后会有多个IP值,其中第一个IP才是真实IP,所以不能通过
request.getRemoteAddr()获取IP地址,如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,
而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP才是用户真实IP地址。
public static String getIpAddress(HttpServletRequest request) {
String ip = null;
try {
String xip = request.getHeader("X-Real-IP");
String xfor = request.getHeader("X-Forwarded-For");
if (StringUtils.isNotEmpty(xfor) && !"unKnown".equalsIgnoreCase(xfor)) {
int index = xfor.indexOf(",");
if (index >= 0) {
return xfor.substring(0, index);
} else {
return xfor;
}
}
ip = xip;
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) {
e.printStackTrace();
}
return ip;
}
2、根据ip地址获取地终端的Mac地址
public static String getMacAddress(String ip){
String macAddress = "";
macAddress = getMacInWindows(ip).trim();
if(macAddress==null||"".equals(macAddress)){
macAddress = getMacInlinux(ip).trim();
}
return macAddress.toUpperCase();
}
public static String getMacInWindows(final String ip){
String result = "";
String[] cmd = {"cmd", "/c", "ping " + ip};
String[] another = {"cmd", "/c", "arp -a"};
String cmdResult = callCmd(cmd,another);
result = filterMacAddress(ip,cmdResult,"-");
return result;
}
public static String getMacInlinux( String ip){
String result = "";
String[] cmd = {"/bin/sh","-c","ping " + ip + " -c 2 && arp -a"};
String cmdResult = callCmd(cmd);
result = filterMacAddress(ip,cmdResult,":");
return result;
}
public static String callCmd(String[] cmd) {
String result = "";
String line = "";
try {
Process proc = Runtime.getRuntime().exec(cmd);
InputStreamReader is = new InputStreamReader(proc.getInputStream());
BufferedReader br = new BufferedReader (is);
while ((line = br.readLine ()) != null) {
result += line;
}
}
catch(Exception e) {
e.printStackTrace();
}
return result;
}
public static String callCmd(String[] cmd,String[] another) {
String result = "";
String line = "";
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
proc.waitFor();
proc = rt.exec(another);
InputStreamReader is = new InputStreamReader(proc.getInputStream());
BufferedReader br = new BufferedReader (is);
while ((line = br.readLine ()) != null) {
result += line;
}
}catch(Exception e) {
e.printStackTrace();
}
return result;
}
public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) {
String result = "";
String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})";
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(sourceString);
while(matcher.find()){
result = matcher.group(1);
if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1)){
break;
}
}
return result;
}