How a Java WebStart Application Get the MAC Address to Access My Web Page

I am writing a Java WebStart deployment application from a website so that users can click and run my software. I need to have a unique machine identification to avoid file access abuse. I would like to use the client MAC address as a unique key so that the server can guarantee that the client does not load too much.

Of course, a user can have several network cards, and how can my Java application determine the MAC address of the network card that the user uses to access my website?

+6
source share
2 answers

You can use java.net.NetworkInterface.getNetworkInterfaces to get the network interfaces and call getHardwareAddress () to get the MAC address.

You can filter the loopback with if.isLoopBack () (where "if" is the interface object). Also filter out any interface where if.getHardwareAddress () returns null. Then select one. You can sort them by name, if.getName (), and take the first one. For your purposes, this does not really matter if it is the actual interface used to download your files or not, just so that you somehow identify the computer. Finally, if.getHardwareAddress () gives you an array of bytes with a MAC address. If you prefer String, format each byte with "% 02x" .format (byte) and append them to the ":" delimiter.

As suggested in another answer, it is better to use a PersistenceService.

However, using a MAC address can be useful if you want to store different data for the same user on different computers in the event that the user has the same / homedirs files on each computer. You can use the MAC address as part of the URL that you pass to PersistenceService # create () and get (). Useful if you need data for each computer, and not data for each user.

A brief example of Scala -code:

def computerID: String = { try { // mac address of first network interface return java.net.NetworkInterface.getNetworkInterfaces .filter(!_.isLoopback) .filter(_.getHardwareAddress != null) .toList.sortBy(_.getName).head .getHardwareAddress.map("%02x".format(_)).mkString(":") } catch { case _ => return "0" // no mac address available? use default "0" } } 
+6
source

.. machine identification ..

Why not do "session identification" instead? Like every application. (You can implement SingleInstanceService to force a single application on a PC.) contact the server to create a unique session. Use this to identify it for each request.

To prevent the user from "using" their manual and stop / restart the application. (over time), save some data using PersistenceService .

+7
source

Source: https://habr.com/ru/post/905728/


All Articles