JSF/JAVA 依据IP获取客户端Mac地址

JSF/JAVA 根据IP获取客户端Mac地址需要对用户的 ip 和 mac 地址进行验证,这里用到获取客户端ip和mac地址的

JSF/JAVA 根据IP获取客户端Mac地址

需要对用户的 ip 和 mac 地址进行验证,这里用到获取客户端ip和mac地址的两个方法,留存。?

1.获取客户端ip地址( 这个必须从客户端传到后台):?
?? jsp页面下,很简单,request.getRemoteAddr() ;?
?? 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换?

Java代码?
  1. public?String?getMyIP()?{??
  2. ?????try?{??
  3. ?????????FacesContext?fc?=?FacesContext.getCurrentInstance();??
  4. ?????????HttpServletRequest?request?=?(HttpServletRequest)fc.getExternalContext().getRequest();??
  5. ?????????return?request.getRemoteAddr();??
  6. ?????}??
  7. ?????catch?(Exception?e)?{??
  8. ?????????e.printStackTrace();??
  9. ?????}??
  10. ?????return?"";??
  11. ?}??



2.获取客户端mac地址?
??? 调用window的命令,在后台Bean里实现 通过ip来获取mac地址。方法如下:

???Java代码?
  1. public?String?getMACAddress(String?ip){??
  2. ????????String?str?=?"";??
  3. ????????String?macAddress?=?"";??
  4. ????????try?{??
  5. ????????????Process?p?=?Runtime.getRuntime().exec("nbtstat?-A?"?+?ip);??
  6. ????????????InputStreamReader?ir?=?new?InputStreamReader(p.getInputStream());??
  7. ????????????LineNumberReader?input?=?new?LineNumberReader(ir);??
  8. ????????????for?(int?i?=?1;?i?<?100;?i++)?{??
  9. ????????????????str?=?input.readLine();??
  10. ????????????????if?(str?!=?null)?{??
  11. ????????????????????if?(str.indexOf("MAC?Address")?>?1)?{??
  12. ????????????????????????macAddress?=?str.substring(str.indexOf("MAC?Address")?+?14,?str.length());??
  13. ????????????????????????break;??
  14. ????????????????????}??
  15. ????????????????}??
  16. ????????????}??
  17. ????????}?catch?(IOException?e)?{??
  18. ????????????e.printStackTrace(System.out);??
  19. ????????}??
  20. ????????return?macAddress;??
  21. ????}??



完整代码:

Java代码?
  1. import?java.io.IOException;???
  2. import?java.io.InputStreamReader;???
  3. import?java.io.LineNumberReader;??
  4. ??
  5. public?class?GetMACAddress?{???
  6. public?String?getMACAddress(String?ipAddress)?{???
  7. String?str?=?"",?strMAC?=?"",?macAddress?=?"";???
  8. try?{???
  9. Process?pp?=?Runtime.getRuntime().exec("nbtstat?-a?"?+?ipAddress);???
  10. InputStreamReader?ir?=?new?InputStreamReader(pp.getInputStream());???
  11. LineNumberReader?input?=?new?LineNumberReader(ir);???
  12. for?(int?i?=?1;?i?<?100;?i++)?{???
  13. str?=?input.readLine();???
  14. if?(str?!=?null)?{???
  15. if?(str.indexOf("MAC?Address")?>?1)?{???
  16. strMAC?=?str.substring(str.indexOf("MAC?Address")?+?14,???
  17. str.length());???
  18. break;???
  19. }???
  20. }???
  21. }???
  22. }?catch?(IOException?ex)?{???
  23. return?"Can't?Get?MAC?Address!";???
  24. }???
  25. //???
  26. if?(strMAC.length()?<?17)?{???
  27. return?"Error!";???
  28. }??
  29. ??
  30. macAddress?=?strMAC.substring(0,?2)?+?":"?+?strMAC.substring(3,?5)???
  31. +?":"?+?strMAC.substring(6,?8)?+?":"?+?strMAC.substring(9,?11)???
  32. +?":"?+?strMAC.substring(12,?14)?+?":"???
  33. +?strMAC.substring(15,?17);???
  34. //???
  35. return?macAddress;???
  36. }??
  37. ??
  38. public?static?void?main(String[]?args)?{???
  39. GetMACAddress?getMACAddress?=?new?GetMACAddress();???
  40. System.out.println(getMACAddress.getMACAddress("59.78.63.38"));?//获得该ip地址的mac地址???
  41. }??
  42. ??
  43. public?static?String?procAll(String?str)?{???
  44. return?procStringEnd(procFirstMac(procAddress(str)));???
  45. }??
  46. ??
  47. public?static?String?procAddress(String?str)?{???
  48. int?indexof?=?str.indexOf("Physical?Address");???
  49. if?(indexof?>?0)?{???
  50. return?str.substring(indexof,?str.length());???
  51. }???
  52. return?str;???
  53. }??
  54. ??
  55. public?static?String?procFirstMac(String?str)?{???
  56. int?indexof?=?str.indexOf(":");???
  57. if?(indexof?>?0)?{???
  58. return?str.substring(indexof?+?1,?str.length()).trim();???
  59. }???
  60. return?str;???
  61. }??
  62. ??
  63. public?static?String?procStringEnd(String?str)?{???
  64. int?indexof?=?str.indexOf("\r");???
  65. if?(indexof?>?0)?{???
  66. return?str.substring(0,?indexof).trim();???
  67. }???
  68. return?str;???
  69. }???
  70. }??

?


只要写一个servlet来调用这个类的getMACAddress(String netip)方法就可以获得客户端的mac地址了,相信你会写jsp应该对servlet也不陌生吧,在jsp中调用servlet通过session传递返回的mac地址,加以判断就可以了?
mac地址是可以通过注册表修改的,不建议以此来作为限制依据~?

?

补充:?
关于获取IP地址的方式,最近在linux下有一个教训,如果单纯通过InetAddress来获取IP地址,就会出现在不同的机器上IP地址不同的问题。?
InetAddress.getLocalHost().getAddress() 实际上是根据hostname来获取IP地址的。linux系统在刚刚装完默认的hostname是localhost,所以通过上面代码获取到的本机 ip就是127.0.0.1, 相对应,比如我的hostname就是rjlin.atsig.com 返回的ip地址确是atsig.com的地址。暂时采用下面代码来处理,当然还不够灵活:?
public static byte[] getIp() throws UnknownHostException {?
byte[] b = InetAddress.getLocalHost().getAddress();?
Enumeration allNetInterfaces = null;?
try {?
allNetInterfaces = NetworkInterface.getNetworkInterfaces();?
} catch (SocketException e) {?
e.printStackTrace();?
}?
InetAddress ip = null;?
NetworkInterface netInterface = null;?
while (allNetInterfaces.hasMoreElements()) {?
netInterface = (NetworkInterface) allNetInterfaces.nextElement();?
if (netInterface.getName().trim().equals("eth0")){?
Enumeration addresses = netInterface.getInetAddresses();?
while (addresses.hasMoreElements()) {?
ip = (InetAddress) addresses.nextElement();?
}?
break;?
}?
}?
if (ip != null && ip instanceof Inet4Address) {?
return b = ip.getAddress();?
}?
return b;?
}

?

补充:

//?获取真实IP的方法(). ? ? ? ? ? ? ? ? ? ? ? ? ? ?

public String getIpAddr(HttpServletRequest request) {
?????? String ip = request.getHeader("x-forwarded-for");
?????? if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
?????????? ip = request.getHeader("Proxy-Client-IP");
?????? }
?????? if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
?????????? ip = request.getHeader("WL-Proxy-Client-IP");
?????? }
?????? if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
?????????? ip = request.getRemoteAddr();
?????? }
?????? return ip;
?? }

?

?

?

?

http://justsee.iteye.com/blog/803316

?

?

?

?