java - 如何同时生成UniqueID?

标签 java

嗨,我有一个要求,我需要生成 SessionID 之类的东西,但也是同时生成的。我正在考虑从 tomcat 7 源“SessionIdGenerator”获取代码。但不确定是否会在不做任何修改的情况下并发生成uniqueID。

类本身在这里:

公共(public)类 SessionIdGenerator {

private Logger logger = LoggerFactory.getLogger();



/**
 * Queue of random number generator objects to be used when creating session
 * identifiers. If the queue is empty when a random number generator is
 * required, a new random number generator object is created. This is
 * designed this way since random number generators use a sync to make them
 * thread-safe and the sync makes using a a single object slow(er).
 */
private Queue<SecureRandom> randoms = new ConcurrentLinkedQueue<SecureRandom>();


/**
 * The Java class name of the secure random number generator class to be
 * used when generating session identifiers. The random number generator
 * class must be self-seeding and have a zero-argument constructor. If not
 * specified, an instance of {@link SecureRandom} will be generated.
 */
private String secureRandomClass = null;


/**
 * The name of the algorithm to use to create instances of
 * {@link SecureRandom} which are used to generate session IDs. If no
 * algorithm is specified, SHA1PRNG is used. To use the platform default
 * (which may be SHA1PRNG), specify the empty string. If an invalid
 * algorithm and/or provider is specified the {@link SecureRandom} instances
 * will be created using the defaults. If that fails, the {@link
 * SecureRandom} instances will be created using platform defaults.
 */
private String secureRandomAlgorithm = "SHA1PRNG";


/**
 * The name of the provider to use to create instances of
 * {@link SecureRandom} which are used to generate session IDs. If
 * no algorithm is specified the of SHA1PRNG default is used. If an invalid
 * algorithm and/or provider is specified the {@link SecureRandom} instances
 * will be created using the defaults. If that fails, the {@link
 * SecureRandom} instances will be created using platform defaults.
 */
private String secureRandomProvider = null;


/** Node identifier when in a cluster. Defaults to the empty string. */
private String jvmRoute = "";


/** Number of bytes in a session ID. Defaults to 16. */
private int sessionIdLength = 16;


/**
 * Specify a non-default @{link {@link SecureRandom} implementation to use.
 * 
 * @param secureRandomClass The fully-qualified class name
 */
public void setSecureRandomClass(String secureRandomClass) {
    this.secureRandomClass = secureRandomClass;
}


/**
 * Specify a non-default algorithm to use to generate random numbers.
 * 
 * @param secureRandomAlgorithm The name of the algorithm
 */
public void setSecureRandomAlgorithm(String secureRandomAlgorithm) {
    this.secureRandomAlgorithm = secureRandomAlgorithm;
}


/**
 * Specify a non-default provider to use to generate random numbers.
 * 
 * @param secureRandomProvider  The name of the provider
 */
public void setSecureRandomProvider(String secureRandomProvider) {
    this.secureRandomProvider = secureRandomProvider;
}


/**
 * Specify the node identifier associated with this node which will be
 * included in the generated session ID.
 * 
 * @param jvmRoute  The node identifier
 */
public void setJvmRoute(String jvmRoute) {
    this.jvmRoute = jvmRoute;
}


/**
 * Specify the number of bytes for a session ID
 * 
 * @param sessionIdLength   Number of bytes
 */
public void setSessionIdLength(int sessionIdLength) {
    this.sessionIdLength = sessionIdLength;
}


/**
 * Generate and return a new session identifier.
 */
public String generateSessionId() {

    byte random[] = new byte[16];

    // Render the result as a String of hexadecimal digits
    StringBuilder buffer = new StringBuilder();

    int resultLenBytes = 0;

    while (resultLenBytes < sessionIdLength) {
        getRandomBytes(random);
        for (int j = 0;
        j < random.length && resultLenBytes < sessionIdLength;
        j++) {
            byte b1 = (byte) ((random[j] & 0xf0) >> 4);
            byte b2 = (byte) (random[j] & 0x0f);
            if (b1 < 10)
                buffer.append((char) ('0' + b1));
            else
                buffer.append((char) ('A' + (b1 - 10)));
            if (b2 < 10)
                buffer.append((char) ('0' + b2));
            else
                buffer.append((char) ('A' + (b2 - 10)));
            resultLenBytes++;
        }
    }

    if (jvmRoute != null && jvmRoute.length() > 0) {
        buffer.append('.').append(jvmRoute);
    }

    return buffer.toString();
}


private void getRandomBytes(byte bytes[]) {

    SecureRandom random = randoms.poll();
    if (random == null) {
        random = createSecureRandom();
    }
    random.nextBytes(bytes);
    randoms.add(random);
}


/**
 * Create a new random number generator instance we should use for
 * generating session identifiers.
 */
private SecureRandom createSecureRandom() {

    SecureRandom result = null;

    long t1 = System.currentTimeMillis();
    if (secureRandomClass != null) {
        try {
            // Construct and seed a new random number generator
            Class<?> clazz = Class.forName(secureRandomClass);
            result = (SecureRandom) clazz.newInstance();
        } catch (Exception e) {
            logger.debug("exception:"+e);
        }
    }

    if (result == null) {
        // No secureRandomClass or creation failed. Use SecureRandom.
        try {
            if (secureRandomProvider != null &&
                    secureRandomProvider.length() > 0) {
                result = SecureRandom.getInstance(secureRandomAlgorithm,
                        secureRandomProvider);
            } else if (secureRandomAlgorithm != null &&
                    secureRandomAlgorithm.length() > 0) {
                result = SecureRandom.getInstance(secureRandomAlgorithm);
            }
        } catch (NoSuchAlgorithmException e) {
            logger.debug("exception:"+e);
        } catch (NoSuchProviderException e) {
            logger.debug("exception:"+e);
        }
    }

    if (result == null) {
        // Invalid provider / algorithm
        try {
            result = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            logger.debug("exception:"+e);
        }
    }

    if (result == null) {

        // Nothing works - use platform default
        result = new SecureRandom();
    }

    // Force seeding to take place
    result.nextInt();

    long t2=System.currentTimeMillis();
    if( (t2-t1) > 100 )
      logger.debug("sessionIdGenerator algorithm:"+result.getAlgorithm()+"time taken:" +Long.valueOf(t2-t1));
    return result;
}

}

最佳答案

我会使用java.util.UUID.randomUUID()来代替。非常简单并保证唯一性。

关于java - 如何同时生成UniqueID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12284430/

相关文章:

Java多线程处理一个字段

Java:如何排除 java.lang 类的隐式导入

java - 如何解析JUnit结果xml文件并在java代码中获取测试用例?

java - ffmpeg 安卓 java.无法使用单引号和正引号

java - JPA与同一实体的双重关系

java - 当我在较小的设备上运行程序时,如何删除 imageView

java - 添加 OnClickListener 会使应用程序崩溃

java - 不显示对象标签,仅显示其 JAXB 属性

java - 我怎样才能在Hibernate中调用这种函数呢?

java - 有没有办法对 Swing 应用程序进行模糊测试?