How does the Java webstart application get the MAC address to access my web page

I am writing a Java webstart application to deploy from the website so that users can click and run my software I need a unique machine identification to avoid abusive file access I want to use the MAC address of the client as the unique key so that the server can ensure that no client downloads too much

Of course, users may have multiple network cards, so how does my java application determine the MAC address of the network card used by users to access my website?

Solution

You can use Java net. NetworkInterface. Getnetworkinterfaces gets the network interfaces and calls gethardwareaddress() on them to get the MAC address

You may want to use if Isloopback() filters out loopbacks (where "if" is an interface object) Also filter out if Gethardwareaddress() returns any interface that is null Then pick one You can press the name if Getname () sorts them and takes the first one For your purposes, it doesn't matter whether it is the actual interface for downloading files, but you can recognize the computer in some way Finally, if Gethardwareaddress () provides you with a byte array containing MAC addresses If you prefer to use string, use "? X" Format (byte) formats each byte and connects them using ":" as a separator

As suggested in another answer, it may be better to use persistence service

However, it may be useful to use MAC addresses if you want to keep different data for the same user on different computers with the same files / homedirs on each computer You can use the MAC address as part of the URL passed to persistenceservice #create() and get() This is useful if you need per computer data instead of per user data

Short sample 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"
  }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>