2010-12-17 95 views
0

我想要創建一個應用程序,其中Web服務器可以獲取登錄的客戶端的MAC地址。我能想到的唯一可能的方式是創建一個JAVA小程序,它包含java.net方法來查找mac地址使用Java小程序在網頁上獲取MAC地址

我正在使用javascript調用applet方法,但瀏覽器不允許這些方法執行。以下是我創建的小程序。

import java.applet.*; 
import java.awt.*; 
import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.UnknownHostException; 

public class AppletRunner extends Applet{ 
// The method that will be automatically called when the applet is started 
    public void init() 
    { 
// It is required but does not need anything. 
    } 


//This method gets called when the applet is terminated 
//That's when the user goes to another page or exits the browser. 
    public void stop() 
    { 
    // no actions needed here now. 
    } 


//The standard method that you have to use to paint things on screen 
//This overrides the empty Applet method, you can't called it "display" for example. 

    public void paint(Graphics g) 
    { 
//method to draw text on screen 
// String first, then x and y coordinate. 
    g.drawString(getMacAddr(),20,20); 
    g.drawString("Hello World",20,40); 

    } 
    public String getMacAddr() { 
    String macAddr= ""; 
    InetAddress addr; 
try { 
    addr = InetAddress.getLocalHost(); 

     System.out.println(addr.getHostAddress()); 
     NetworkInterface dir = NetworkInterface.getByInetAddress(addr); 
     byte[] dirMac = dir.getHardwareAddress(); 

     int count=0; 
     for (int b:dirMac){ 
     if (b<0) b=256+b; 
     if (b==0) { 
       macAddr=macAddr.concat("00"); 
     } 
     if (b>0){ 

      int a=b/16; 
      if (a==10) macAddr=macAddr.concat("A"); 
      else if (a==11) macAddr=macAddr.concat("B"); 
      else if (a==12) macAddr=macAddr.concat("C"); 
      else if (a==13) macAddr=macAddr.concat("D"); 
      else if (a==14) macAddr=macAddr.concat("E"); 
      else if (a==15) macAddr=macAddr.concat("F"); 
      else macAddr=macAddr.concat(String.valueOf(a)); 
      a = (b%16); 
      if (a==10) macAddr=macAddr.concat("A"); 
      else if (a==11) macAddr=macAddr.concat("B"); 
      else if (a==12) macAddr=macAddr.concat("C"); 
      else if (a==13) macAddr=macAddr.concat("D"); 
      else if (a==14) macAddr=macAddr.concat("E"); 
      else if (a==15) macAddr=macAddr.concat("F"); 
      else macAddr=macAddr.concat(String.valueOf(a)); 
     } 
     if (count<dirMac.length-1)macAddr=macAddr.concat("-"); 
     count++; 
     } 

} catch (UnknownHostException e) { 
    // TODO Auto-generated catch block 
    macAddr=e.getMessage(); 
} catch (SocketException e) { 
    // TODO Auto-generated catch block 
    macAddr = e.getMessage(); 
} 
return macAddr; 
} 

    } 
+3

你爲什麼要這樣做?我只能想到隱私入侵。 – thejh 2010-12-17 05:23:40

+0

您是否收到錯誤或您如何知道瀏覽器不允許它? – Enrique 2010-12-17 05:24:16

回答

1

我不認爲這是可能的。 Web服務器與客戶端在MAC地址所在的鏈路層之上幾層之間進行通信 - 它被TCP/IP抽象出去,並且沒有理由讓客戶端發送它,除非你專門有客戶端代碼這樣做。

Java代碼不工作的原因是因爲Java沙盒的安全管理器不允許這樣的低級調用 - 它應該這樣做!如果你確實找到了一種方法來讓這件事情起作用(我懷疑你會這麼做),你應該立即向Oracle報告,因爲它根本不應該發生。

我看不出很多的原因,爲什麼你要想要它要麼,說實話。

1

Java applet被禁止訪問客戶端上的這些方法,因爲它運行在受保護的沙箱中。

0

它可能無法在瀏覽器中使用,因爲它違背了沙箱模式。使用特定於瀏覽器的本機代碼擴展可能會帶來一些好運。

但是,重要的例外情況是,如果您的Web服務器與客戶端在同一個局域網(同一個交換機)中 - 那麼客戶端的MAC地址對於服務器是已知的,因爲它仍然存在於IP包。

4

出於安全原因,Applets無法正常訪問這些功能。爲避免這些限制,您需要一個signed applet以及一個策略文件。

然後,您可以編寫一個策略文件,授予您的小程序訪問其所需功能的權限。如果用戶授予您的小程序必要的權限(它會提示他們),您的小程序可以使用這些功能。

2

在NetBeans中,你可以註冊一個應用程序使Webstart的:

  1. 訪問項目>屬性>應用程序> Webstart的
  2. 選中 「啓用Web Start」。這顯示了一個標題爲簽名的sectin。
  3. 單擊位於簽名部分的「自定義」按鈕。
  4. 選擇「通過生成密鑰進行自簽名」。