How do I get an ID or String which is unique for every user/pc in Java?
I'm looking for a unique ID or String to identify users or PC's. I'm creating a Java script with a security method which uses a unique ID or String. I used the MAC Address before, but开发者_StackOverflow中文版 since an update at the developers I'm coding for, this isn't allowed anymore. Is there another way to get some sort of unique ID to identify users or pc's which is allowed? For more information: I'm coding for RSBot, which is an open-source program which runs user created Java scripts.
Thanks in advance, Kevin.
See java.util.UUID
, specifically:
UUID.randomUUID();
http://download.oracle.com/javase/1,5.0/docs/api/java/util/UUID.html#randomUUID()
Note that this is random, but not reproducible. If you need to be able to generate the same ID over and over on the same computer this is not what you need.
You may also want to look at JUG -
GUUIDs based on the Ethernet device, random, time, etc
http://wiki.fasterxml.com/JugHome
OSHI is a free JNA-based (native) Operating System and Hardware Information library for Java. It does not require the installation of any additional native libraries. Maven dependency:
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.13.0</version>
<scope>compile</scope>
</dependency>
You can retrieve system information, such as OS id, CPU id, baseboard id etc. Then just combine id-s to generate PC UUID:
public String getUID() {
final byte[] rawInput = StringUtils
.joinWith(hardwareLayer.getProcessor().getProcessorID(),
hardwareLayer.getComputerSystem().getBaseboard().getSerialNumber(),
... )
.getBytes(StandardCharsets.UTF_8);
try {
return UUID.nameUUIDFromBytes(rawInput).toString().toUpperCase();
} catch (final Exception e) {
logger.error(e);
return StringUtils.EMPTY;
}
}
精彩评论