Javascript 使用 Java 小程序获取网页上的 MAC 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4467905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting MAC address on a web page using a Java applet
提问by Amit Arya
I want to create an application where a web server can get the MAC Address of the clients logging in. The only possible way I could think of was to create a JAVA applet which contains java.net methods to find the mac address
我想创建一个应用程序,其中 Web 服务器可以获取登录客户端的 MAC 地址。我能想到的唯一可能的方法是创建一个 JAVA 小程序,其中包含用于查找 mac 地址的 java.net 方法
I am using javascript to call the applet methods, but the browser is not allowing those methods to execute. Below is the applet I have created.
我正在使用 javascript 调用小程序方法,但浏览器不允许执行这些方法。下面是我创建的小程序。
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;
}
}
回答by sleske
Applets cannot normally access these functions for security reasons. To avoid these restrictions, you need a signed applet, along with a policy file.
出于安全原因,小程序通常无法访问这些功能。为了避免这些限制,您需要一个签名的小程序以及一个策略文件。
You can then write a policy file which grants your applet access to the functionality it needs. If the user then grants your applet the necessary permissions (it will prompt for them), your applet can use the functions.
然后,您可以编写一个策略文件,授予您的小程序访问所需功能的权限。如果用户随后授予您的小程序必要的权限(它会提示他们),您的小程序就可以使用这些功能。
回答by Optim
In Netbeans, you can sign an application enabling the WebStart:
在 Netbeans 中,您可以对启用 WebStart 的应用程序进行签名:
- Access to Your project > properties > Application > WebStart
- Check "Enable Web Start". This show a sectin titled signing.
- Click the "Customize" button located in the signing section.
- Select "self-sign by generated key".
- 访问您的项目 > 属性 > 应用程序 > WebStart
- 选中“启用 Web Start”。这显示了一个名为签名的节。
- 单击位于签名部分的“自定义”按钮。
- 选择“通过生成的密钥自签名”。
回答by Adrian Petrescu
I don't think this will be possible. Web servers communicate with clients several layers above the link layer where MAC addresses live -- it's abstracted away by TCP/IP and there's no reason for the client to send it unless you specifically have client code to do that.
我不认为这是可能的。Web 服务器在 MAC 地址所在的链路层之上的几层与客户端进行通信——它被 TCP/IP 抽象出来,除非你特别有客户端代码来发送它,否则客户端没有理由发送它。
The reason your Java code isn't working is because the Java sandbox's security manager disallows such low-level calls -- which it should! If you ever do find a way to get that thing to work (which I doubt you will) you should promptly report it to Oracle because it shouldn't be happening at all.
您的 Java 代码不工作的原因是 Java 沙箱的安全管理器不允许这种低级调用——它应该这样做!如果您确实找到了一种方法来让那件事发挥作用(我怀疑您会这样做),您应该立即将其报告给 Oracle,因为它根本不应该发生。
I can't see much of a reason why you'd wantit either, to be honest.
老实说,我也看不出你想要它的任何理由。
回答by Philip Fourie
The Java applet is prevented to access those methods on the client because it runs in a protected sandbox.
Java 小程序无法访问客户端上的这些方法,因为它在受保护的沙箱中运行。
回答by qdot
It might not be possible within a browser, since it is against the sandboxing paradigm. You might have some luck with browser-specific native code extensions.
这在浏览器中可能是不可能的,因为它违反了沙盒范式。您可能对特定于浏览器的本机代码扩展有一些运气。
However, the important exception is if your web server is in the same local area network (same switch) as the client - then, the MAC address of the client is known to the server because it is still present in the IP packet.
但是,重要的例外是,如果您的 Web 服务器与客户端位于同一个局域网(同一交换机)中,那么服务器就知道客户端的 MAC 地址,因为它仍然存在于 IP 数据包中。