最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting MAC address on a web page using a Java applet - Stack Overflow

programmeradmin3浏览0评论

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 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.InetAddress;
import java.NetworkInterface;
import java.SocketException;
import java.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;
 }

  }

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 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.InetAddress;
import java.NetworkInterface;
import java.SocketException;
import java.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;
 }

  }
Share Improve this question edited Jun 19, 2012 at 12:59 mak 34 bronze badges asked Dec 17, 2010 at 5:18 Amit AryaAmit Arya 211 gold badge1 silver badge3 bronze badges 2
  • 3 Why do you want that? I can only think of privacy invasion. – thejh Commented Dec 17, 2010 at 5:23
  • Are you getting an error or how do you know the browser is not allowing it? – Enrique Commented Dec 17, 2010 at 5:24
Add a ment  | 

5 Answers 5

Reset to default 5

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 municate 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.

The Java applet is prevented to access those methods on the client because it runs in a protected sandbox.

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.

发布评论

评论列表(0)

  1. 暂无评论