【问题标题】:How to get uuid or mac address from client in Java?如何在 Java 中从客户端获取 uuid 或 mac 地址?
【发布时间】:2015-07-19 06:01:16
【问题描述】:

我正在为基于 Java 的 Web 应用程序寻找唯一标识客户端的解决方案。服务器与客户端在同一个网络中,我认为使用 MAC 地址将是一个很好的解决方案。 问题是我不能使用 cookie,因为它们可以在客户端被删除,而且我不能使用 IP,因为它们只能发出新的 DHCP 租约更新。

所以我想回退到客户端的 MAC 地址。我知道没有 java 内置功能来获取 MAC 地址。 是否有一个库可以处理每个操作系统的输出? (主要的 Windows 和 Mac),因为我的 java 应用程序在两个平台上运行。

或者是否有任何其他建议可以在网站和 HTTP 协议中唯一标识客户端? (可能是 HTML5 数据存储或其他)

我正在使用 Java 1.7 顺便说一句。

我不会强迫用户登录或以其他方式表明自己的身份,也不会为客户的智能手机编写本机应用程序。

【问题讨论】:

  • 这是答案:“如何在 Java 中获取唯一的计算机标识符(如磁盘 ID 或主板 ID)”(stackoverflow.com/questions/1986732/…
  • 我正在寻找仅使用网络服务的客户的 uuid。 (不涉及客户端上的 Java 小程序),您的链接仅涉及本地计算机。

标签: java web-applications uuid mac-address


【解决方案1】:

我编写了自己的方法来解决我的问题。如果有人需要代码来在同一网络中查找 MAC 地址,这里就是。在 Win 7 和 Mac OS X 10.8.2 上为我工作,无需任何管理员权限

Pattern macpt = null;

private String getMac(String ip) {

    // Find OS and set command according to OS
    String OS = System.getProperty("os.name").toLowerCase();

    String[] cmd;
    if (OS.contains("win")) {
        // Windows
        macpt = Pattern
                .compile("[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+");
        String[] a = { "arp", "-a", ip };
        cmd = a;
    } else {
        // Mac OS X, Linux
        macpt = Pattern
                .compile("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+");
        String[] a = { "arp", ip };
        cmd = a;
    }

    try {
        // Run command
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        // read output with BufferedReader
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line = reader.readLine();

        // Loop trough lines
        while (line != null) {
            Matcher m = macpt.matcher(line);

            // when Matcher finds a Line then return it as result
            if (m.find()) {
                System.out.println("Found");
                System.out.println("MAC: " + m.group(0));
                return m.group(0);
            }

            line = reader.readLine();
        }

    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // Return empty string if no MAC is found
    return "";
}

【讨论】:

  • 这不能保证,因为“为了能够 ARP 设备,您必须在该网络上的该交换机上有一个接口 (SVI)。要 ARP 设备,您必须有一个带有同一个第 2 层 vlan 上的第 3 层 IP。”另一种方法是使用“getmac /s 00.00.00.00”,其中 00.00.00.00 是远程 IP 地址
  • @Nexus2K 我使用了你的代码,它在 windows 系统上可以正常工作,但在 linux 上它不能正常工作
  • @KVK 它适用于我在 linux mint 下。如果您从127.0.0.1 请求,它将找不到任何东西。实施吧!
【解决方案2】:

我能找到的最好的是:Query ARP cache to get MAC ID

总结如下:

  • 没有标准的 Java API,
  • 没有独立于操作系统的解决方案,
  • 您的应用程序通常需要获得特权(例如 root 访问权限)才能查询主机的 ARP 缓存,并且
  • 如果数据包通过网络路由器,您将无法再识别源 MAC 地址。

我认为这不是识别用户机器的好方法。

还要考虑:

  • 这仅识别机器,而不是用户。一些计算机由多个用户共享。
  • MAC 地址也可以更改。

【讨论】:

  • 有关客户的更多信息。它们都是智能手机,不会被多个用户共享。更改智能手机的 MAC 地址比删除 cookie 要困难得多。
  • 正如问题中所述,所有客户端都在同一个网络中
【解决方案3】:

IP 地址的使用在本地网络中不起作用。我使用了其他一些方法来获取 MAC 地址 - sysout 解析有用的命令。

public String getMacAddress() throws Exception {
    String macAddress = null;
    String command = "ifconfig";

    String osName = System.getProperty("os.name");
    System.out.println("Operating System is " + osName);

    if (osName.startsWith("Windows")) {
        command = "ipconfig /all";
    } else if (osName.startsWith("Linux") || osName.startsWith("Mac") || osName.startsWith("HP-UX")
            || osName.startsWith("NeXTStep") || osName.startsWith("Solaris") || osName.startsWith("SunOS")
            || osName.startsWith("FreeBSD") || osName.startsWith("NetBSD")) {
        command = "ifconfig -a";
    } else if (osName.startsWith("OpenBSD")) {
        command = "netstat -in";
    } else if (osName.startsWith("IRIX") || osName.startsWith("AIX") || osName.startsWith("Tru64")) {
        command = "netstat -ia";
    } else if (osName.startsWith("Caldera") || osName.startsWith("UnixWare") || osName.startsWith("OpenUNIX")) {
        command = "ndstat";
    } else {// Note: Unsupported system.
        throw new Exception("The current operating system '" + osName + "' is not supported.");
    }

    Process pid = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
    Pattern p = Pattern.compile("([\\w]{1,2}(-|:)){5}[\\w]{1,2}");
    while (true) {
        String line = in.readLine();
        System.out.println("line " + line);
        if (line == null)
            break;

        Matcher m = p.matcher(line);
        if (m.find()) {
            macAddress = m.group();
            break;
        }
    }
    in.close();
    return macAddress;
}

这应该适用于任何地方。至少,在 Ubuntu 机器上使用这种方法会得到以下结果:

Operating System is Linux
line eth0      Link encap:Ethernet  HWaddr f4:6d:04:63:8e:21  
mac: f4:6d:04:63:8e:21

【讨论】:

  • 请重新阅读我的问题,我需要远程计算机的 MAC...不是我自己的。
猜你喜欢
  • 2012-03-14
  • 2016-07-27
  • 2023-04-02
  • 2017-10-28
  • 1970-01-01
  • 2019-07-12
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多