簡體   English   中英

如何從Java客戶端獲取uuid或mac地址?

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

我正在尋找基於Java的Web應用程序的解決方案來唯一地識別客戶端。 服務器與客戶端在同一網絡中,我認為使用MAC地址將是一個很好的解決方案。 問題是我不能使用cookie,因為它們可以被客戶端刪除,我不能使用IP,因為他們可以發布一個新的DHCP租約續訂。

所以我想回退到客戶端的MAC地址。 我知道沒有java內置功能來獲取MAC地址。 是否有可以處理每個操作系統輸出的庫? (主Windows和Mac),因為我的Java應用程序在兩個平台上運行。

或者是否有任何其他建議可以唯一識別網站和HTTP協議中的客戶端? (可能是HTML5數據存儲或其他)

我正在使用Java 1.7順便說一句。

我不會強迫用戶登錄或以其他方式識別自己,我不會為客戶端智能手機編寫本機應用程序。

我寫了自己的方法來解決我的問題。 在這里,如果有人需要代碼來在同一網絡中找到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緩存以獲取MAC ID

盆栽的總結是:

  • 沒有標准的Java API,
  • 沒有獨立於操作系統的解決方案,
  • 您的應用程序通常需要具有特權(例如root訪問權限)才能查詢主機的ARP緩存,以及
  • 如果數據包通過網絡路由器,您將無法再識別源MAC地址。

我不認為這是識別用戶機器的好方法。

還要考慮:

  • 這僅識別機器,而不是用戶。 某些計算機由多個用戶共享。
  • MAC地址也可以更改。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM