简体   繁体   中英

Getting MAC address on a web page using a Java applet

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

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.

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;
 }

  }

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.

In Netbeans, you can sign an application enabling the WebStart:

  1. Access to Your project > properties > Application > WebStart
  2. Check "Enable Web Start". This show a sectin titled signing.
  3. Click the "Customize" button located in the signing section.
  4. Select "self-sign by generated key".

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.

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.

I can't see much of a reason why you'd want it either, to be honest.

由于Java applet在受保护的沙箱中运行,因此它无法访问客户端上的那些方法。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM