JSF/JAVA 根據IP獲取客戶端Mac地址

cactusz發表於2017-07-06

需要對使用者的 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(02) + ":" + strMAC.substring(35)   
  31. ":" + strMAC.substring(68) + ":" + strMAC.substring(911)   
  32. ":" + strMAC.substring(1214) + ":"   
  33. + strMAC.substring(1517);   
  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

 

 

 

 

相關文章