2010-12-17 33 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. 选择“通过生成密钥进行自签名”。