From 9e8350fa651a32f7e8e0228bd2b4f8a4ee6508ac Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 19 Oct 2026 11:09:54 +0200 Subject: [PATCH] Use default SecureRandom in identifier generator The OS-specific algorithm list never picked the first available algorithm because the loop did not stop on success. On Linux this always resulted in SHA1PRNG. The JDK default already selects a suitable non-blocking generator per platform and respects java.security configuration, so the list is unnecessary. --- .../core/DefaultIdentifierGenerator.java | 55 +------------------ 1 file changed, 3 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java b/src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java index 6e758784..dcbbb89f 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java +++ b/src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java @@ -15,17 +15,12 @@ */ package org.springframework.data.keyvalue.core; -import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; -import java.util.Arrays; -import java.util.List; import java.util.UUID; -import java.util.concurrent.atomic.AtomicReference; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.core.TypeInformation; import org.springframework.util.ClassUtils; -import org.springframework.util.StringUtils; /** * Default implementation of {@link IdentifierGenerator} to generate identifiers of types {@link UUID}, String, @@ -37,7 +32,7 @@ enum DefaultIdentifierGenerator implements IdentifierGenerator { INSTANCE; - private final AtomicReference secureRandom = new AtomicReference<>(null); + private final SecureRandom secureRandom = new SecureRandom(); @Override @SuppressWarnings("unchecked") @@ -50,57 +45,13 @@ public T generateIdentifierOfType(TypeInformation identifierType) { } else if (ClassUtils.isAssignable(String.class, type)) { return (T) UUID.randomUUID().toString(); } else if (ClassUtils.isAssignable(Integer.class, type)) { - return (T) Integer.valueOf(getSecureRandom().nextInt()); + return (T) Integer.valueOf(this.secureRandom.nextInt()); } else if (ClassUtils.isAssignable(Long.class, type)) { - return (T) Long.valueOf(getSecureRandom().nextLong()); + return (T) Long.valueOf(this.secureRandom.nextLong()); } throw new InvalidDataAccessApiUsageException( String.format("Identifier cannot be generated for %s; Supported types are: UUID, String, Integer, and Long", identifierType.getType().getName())); } - - private SecureRandom getSecureRandom() { - - SecureRandom secureRandom = this.secureRandom.get(); - if (secureRandom != null) { - return secureRandom; - } - - for (String algorithm : OsTools.secureRandomAlgorithmNames()) { - try { - secureRandom = SecureRandom.getInstance(algorithm); - } catch (NoSuchAlgorithmException e) { - // ignore and try next. - } - } - - if (secureRandom == null) { - throw new InvalidDataAccessApiUsageException( - String.format("Could not create SecureRandom instance for one of the algorithms '%s'", - StringUtils.collectionToCommaDelimitedString(OsTools.secureRandomAlgorithmNames()))); - } - - this.secureRandom.compareAndSet(null, secureRandom); - - return secureRandom; - } - - /** - * @author Christoph Strobl - * @since 1.1.2 - */ - private static class OsTools { - - private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name").toLowerCase(); - - private static final List SECURE_RANDOM_ALGORITHMS_LINUX_OSX_SOLARIS = Arrays.asList("NativePRNGBlocking", - "NativePRNGNonBlocking", "NativePRNG", "SHA1PRNG"); - private static final List SECURE_RANDOM_ALGORITHMS_WINDOWS = Arrays.asList("SHA1PRNG", "Windows-PRNG"); - - static List secureRandomAlgorithmNames() { - return OPERATING_SYSTEM_NAME.contains("win") ? SECURE_RANDOM_ALGORITHMS_WINDOWS - : SECURE_RANDOM_ALGORITHMS_LINUX_OSX_SOLARIS; - } - } }