简体   繁体   English

Java MAC地址授权

[英]Java MAC Address Authorization

Basically I want java to retrieve the mac address and open a URL with the Mac Address encoded. 基本上,我希望Java检索mac地址并打开一个带有Mac Address编码的URL。 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. 该URL将利用PHP来检查MAC地址是否在允许MAC地址的列表中(我已经完成了这一部分),并将输出“ true”或Java可以检查的任何内容。 If it's true, Java would change boolean authorized to true. 如果为true,则Java会将布尔授权更改为true。 After that, I can simply just issue an if statement to continue or not. 在那之后,我可以简单地发出一个if语句继续或不继续。

I just need something simple as I'm not a fantastic programmer. 我只需要一些简单的东西,因为我不是一个出色的程序员。 I'm still learning. 我还在学习。

  1. To get the MAC address 获取MAC地址

  2. To URL encode a parameter use URLEncoder#encode(String, String) 若要对参数进行URL编码,请使用URLEncoder#encode(String, String)

Here's an example that will list the name and MAC for network interfaces found in Java 6: 下面的示例将列出Java 6中找到的网络接口的名称和MAC:

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). 您可以轻松获得不止一个(由于VMware网络适配器,我得到了3个)。 It is possible to determine the NetworkInterface for a particular InetAddress . 可以为特定的InetAddress确定NetworkInterface 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. 但是我发现,如果我要连接到VPN,它将给出一个空的MAC地址。 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. 因此,如果要处理的系统配置可能不同,我不确定如何可靠地获取所需的MAC。 Hope this helps though. 希望这会有所帮助。

Once you have the right MAC address you can open a URL to send it to your server like this: 获得正确的MAC地址后,您可以打开一个URL,将其发送到服务器,如下所示:

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

You just should not do this in Java. 您只是不应在Java中执行此操作。 Mac Addess filtering is the domain of firewalls. Mac Addess过滤是防火墙的领域。 If you need to do this, you configure your firewall. 如果需要执行此操作,请配置防火墙。 This at least CAN be done from Java, depending on your firewall. 至少可以通过Java完成此操作,具体取决于您的防火墙。

In Linux, configure your iptables, thanks to command line interfaces this is easy from Java. 在Linux中,通过命令行界面配置iptables,这在Java中很容易实现。

On Windows, there should be an easy to use API for that. 在Windows上,应该有一个易于使用的API。 Use JNA to access it. 使用JNA进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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