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

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

所以我想回退到客户端的MAC地址。 我知道没有java内置function来获取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