Javascript 使用Java小程序在网页上获取MAC地址

Javascript 使用Java小程序在网页上获取MAC地址,javascript,applet,Javascript,Applet,我想创建一个应用程序,其中web服务器可以获取登录客户端的MAC地址。我能想到的唯一可能的方法是创建一个JAVA小程序,其中包含查找mac地址的JAVA.net方法 我使用javascript调用applet方法,但浏览器不允许执行这些方法。下面是我创建的小程序 import java.applet.*; import java.awt.*; import java.net.InetAddress; import java.net.NetworkInterface; import java.ne

我想创建一个应用程序,其中web服务器可以获取登录客户端的MAC地址。我能想到的唯一可能的方法是创建一个JAVA小程序,其中包含查找mac地址的JAVA.net方法

我使用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;
 }

  }
import java.applet.*;
导入java.awt.*;
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.net.UnknownHostException;
公共类AppletRunner扩展Applet{
//启动小程序时将自动调用的方法
公共void init()
{
//它是必需的,但不需要任何东西。
}
//小程序终止时调用此方法
//这是当用户转到另一个页面或退出浏览器时。
公共停车场()
{
//现在不需要采取任何行动。
}
//在屏幕上绘制东西时必须使用的标准方法
//这会覆盖空的Applet方法,例如,您不能将其称为“display”。
公共空间涂料(图g)
{
//在屏幕上绘制文本的方法
//首先是字符串,然后是x和y坐标。
g、 抽绳(getMacAddr(),20,20);
g、 抽绳(“你好,世界”,20,40);
} 
公共字符串getMacAddr(){
字符串macAddr=“”;
地址地址地址;
试一试{
addr=InetAddress.getLocalHost();
System.out.println(addr.getHostAddress());
NetworkInterface dir=NetworkInterface.getByInetAddress(addr);
字节[]dirMac=dir.getHardwareAddress();
整数计数=0;
for(int b:dirMac){
if(b0){
INTA=b/16;
如果(a==10)macAddr=macAddr.concat(“a”);
如果(a==11)macAddr=macAddr.concat(“B”);
如果(a==12)macAddr=macAddr.concat(“C”);
如果(a==13)macAddr=macAddr.concat(“D”);
如果(a==14)macAddr=macAddr.concat(“E”);
如果(a==15)macAddr=macAddr.concat(“F”);
else macAddr=macAddr.concat(String.valueOf(a));
a=(b%16);
如果(a==10)macAddr=macAddr.concat(“a”);
如果(a==11)macAddr=macAddr.concat(“B”);
如果(a==12)macAddr=macAddr.concat(“C”);
如果(a==13)macAddr=macAddr.concat(“D”);
如果(a==14)macAddr=macAddr.concat(“E”);
如果(a==15)macAddr=macAddr.concat(“F”);
else macAddr=macAddr.concat(String.valueOf(a));
}

如果(count我认为这是不可能的。Web服务器与位于MAC地址所在的链接层之上的多个层的客户端进行通信——它被TCP/IP抽象出来,客户端没有理由发送它,除非您有专门的客户端代码

您的Java代码无法工作的原因是Java沙盒的安全管理器不允许此类低级调用——它应该这样做!如果您真的找到了让它工作的方法(我怀疑您会这样做),您应该立即向Oracle报告,因为它根本不应该发生


老实说,我也看不出您想要它的原因。

Java小程序被阻止在客户端访问这些方法,因为它在受保护的沙箱中运行。

在浏览器中可能不可能,因为这与沙箱范例背道而驰。您可能在特定于浏览器的本机代码扩展方面有一些运气


但是,重要的例外是,如果您的web服务器位于同一局域网(同一交换机)中作为客户端-然后,服务器知道客户端的MAC地址,因为它仍然存在于IP数据包中。

出于安全原因,小程序无法正常访问这些功能。为了避免这些限制,您需要一个策略文件和一个策略文件


然后,您可以编写一个策略文件,授予您的小程序对其所需功能的访问权限。如果用户随后授予您的小程序必要的权限(它将提示用户),则您的小程序可以使用这些功能。

在Netbeans中,您可以对启用WebStart的应用程序进行签名:

  • 访问项目>属性>应用程序>WebStart
  • 选中“启用Web启动”。这将显示一个标题为签名的扇区
  • 单击“签名”部分中的“自定义”按钮
  • 选择“通过生成的密钥进行自我签名”

  • 你为什么要这样做?我只能想到侵犯隐私。你是否收到错误,或者你如何知道浏览器不允许这样做?我已经签署了我的小程序,下一步怎么办?我如何才能请求授予用户权限?请帮助me@JayPatel:嗨,请不要在评论中提问。在描述问题的地方问一个新问题。请检查my问题