开发者

Java MAC Address Authorization

Basically I want java to retrieve the mac address and open a URL w开发者_如何转开发ith the Mac Address encoded. The URL would utilize PHP to check if the MAC address is in the list of allow MAC Address (I've already completed this part) and would output "true" or anything Java could check against. If it's true, Java would change boolean authorized to true. After that, I can simply just issue an if statement to continue or not.

I just need something simple as I'm not a fantastic programmer. I'm still learning.


  1. To get the MAC address

    • Java 6
    • Java 5
  2. To URL encode a parameter use URLEncoder#encode(String, String)


Here's an example that will list the name and MAC for network interfaces found in Java 6:

Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
    NetworkInterface ni = e.nextElement();
    if (!ni.isUp()) {
        continue;
    }

    byte[] address = ni.getHardwareAddress();
    if (address == null || address.length == 0) {
        continue;
    }

    StringBuilder sb = new StringBuilder(2 * address.length);
    for (byte b : address) {
        sb.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4));
        sb.append("0123456789ABCDEF".charAt((b & 0x0F)));
        sb.append(":");
    }
    sb.deleteCharAt(sb.length() - 1);
    String mac = sb.toString();

    System.out.println(ni.getName() + " " + mac);
}

You could easily get more than one (I get 3 due to VMware network adapters). It is possible to determine the NetworkInterface for a particular InetAddress. You can query it for localhost and it works:

InetAddress localhost = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(localhost);

However I've discovered that if I'm connecting to a VPN it'll give an empty MAC address. So I'm not entirely sure how to reliably get the MAC you need if you're dealing with systems that could be configured differently. Hope this helps though.

Once you have the right MAC address you can open a URL to send it to your server like this:

new URL("http://server?mac=" + mac).openStream().close();


You just should not do this in Java. Mac Addess filtering is the domain of firewalls. If you need to do this, you configure your firewall. This at least CAN be done from Java, depending on your firewall.

In Linux, configure your iptables, thanks to command line interfaces this is easy from Java.

On Windows, there should be an easy to use API for that. Use JNA to access it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜