如何从Java客户端获取uuid或mac地址? [英] How to get uuid or mac address from client in Java?

查看:155
本文介绍了如何从Java客户端获取uuid或mac地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a solution for a Java based webapplication to uniquely identify the client. The server is in the same network as the clients and I thought that using the MAC address would be a good solution. 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.

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

So I would like to fallback to the MAC address of the clients. I'm aware that there is no java built in feature to get the MAC address. Is there a library that can handle the output of every OS? (primary Windows and Mac) since my java Application runs on both platforms.

还是有任何其他建议可以唯一识别网站和HTTP协议中的客户端吗? (也许是HTML5数据存储或其他东西)

or are there any other suggestions for uniquely identifying a client within a website and the HTTP Protocol ? (maybe HTML5 data stores or something else)

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

I'm using Java 1.7 btw.

我赢了' t强制用户登录或以其他方式识别自己,我不会为客户端智能手机编写本机应用程序。

I won't force the user to login or otherwise identify himself and I won't program a native app for the clients smartphone.

推荐答案

我写了我自己的方法来解决我的问题。在这里,如果有人需要代码来在同一网络中找到MAC地址。适用于我,在Win 7和Mac OS X上没有任何管理员权限10.8.2

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. Works for me without any admin privileges on Win 7 and 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 "";
}

这篇关于如何从Java客户端获取uuid或mac地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆