简体   繁体   English

如何从Java客户端获取uuid或mac地址?

[英]How to get uuid or mac address from client in Java?

I'm looking for a solution for a Java based webapplication to uniquely identify the client. 我正在寻找基于Java的Web应用程序的解决方案来唯一地识别客户端。 The server is in the same network as the clients and I thought that using the MAC address would be a good solution. 服务器与客户端在同一网络中,我认为使用MAC地址将是一个很好的解决方案。 The problem is I can't work with cookies because they can be deleted client-side and I can't use the IP because they could just issue a new DHCP lease renewal. 问题是我不能使用cookie,因为它们可以被客户端删除,我不能使用IP,因为他们可以发布一个新的DHCP租约续订。

So I would like to fallback to the MAC address of the clients. 所以我想回退到客户端的MAC地址。 I'm aware that there is no java built in feature to get the MAC address. 我知道没有java内置功能来获取MAC地址。 Is there a library that can handle the output of every OS? 是否有可以处理每个操作系统输出的库? (primary Windows and Mac) since my java Application runs on both platforms. (主Windows和Mac),因为我的Java应用程序在两个平台上运行。

or are there any other suggestions for uniquely identifying a client within a website and the HTTP Protocol ? 或者是否有任何其他建议可以唯一识别网站和HTTP协议中的客户端? (maybe HTML5 data stores or something else) (可能是HTML5数据存储或其他)

I'm using Java 1.7 btw. 我正在使用Java 1.7顺便说一句。

I won't force the user to login or otherwise identify himself and I won't program a native app for the clients smartphone. 我不会强迫用户登录或以其他方式识别自己,我不会为客户端智能手机编写本机应用程序。

I wrote my own method to solve my issue. 我写了自己的方法来解决我的问题。 Here it is if ever someone needs code to find a MAC address in the same network. 在这里,如果有人需要代码来在同一网络中找到MAC地址。 Works for me without any admin privileges on Win 7 and Mac OS X 10.8.2 适用于我,在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 "";
}

The best I could find is this: Query ARP cache to get MAC ID 我能找到的最好的是: 查询ARP缓存以获取MAC ID

And the potted summary is that: 盆栽的总结是:

  • there is no standard Java API, 没有标准的Java API,
  • there is no operating system independent solution, 没有独立于操作系统的解决方案,
  • your application typically needs to be privileged (eg root access) to query the host's ARP cache, and 您的应用程序通常需要具有特权(例如root访问权限)才能查询主机的ARP缓存,以及
  • if the packets go through a network router, you won't be able to identify the source MAC address anymore. 如果数据包通过网络路由器,您将无法再识别源MAC地址。

I don't think this is a good approach for identifying your user's machine. 我不认为这是识别用户机器的好方法。

Consider also that: 还要考虑:

  • This only identifies the machine, not the user. 这仅识别机器,而不是用户。 Some computers are shared by multiple users. 某些计算机由多个用户共享。
  • MAC addresses can be changed too. MAC地址也可以更改。

Usage of IP address isn't working in the local network . IP地址的使用在本地网络中不起作用 I have used some other method to get a MAC address - sysout parsing of useful commands. 我已经使用了一些其他方法来获取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;
}

This should work everywhere. 这应该适用于所有地方 At least, the usage of this method on Ubuntu machine gives the following result: 至少,在Ubuntu机器上使用此方法会产生以下结果:

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

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

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