Browse Source

MimeTypeUtils lazily initializes SecureRandom for multipart boundary

Issue: SPR-16974
pull/1862/merge
Juergen Hoeller 8 years ago
parent
commit
847202c8f8
  1. 28
      spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

28
spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import org.springframework.lang.Nullable;
import org.springframework.util.MimeType.SpecificityComparator; import org.springframework.util.MimeType.SpecificityComparator;
/** /**
@ -46,8 +47,6 @@ public abstract class MimeTypeUtils {
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'}; 'V', 'W', 'X', 'Y', 'Z'};
private static final Random RND = new SecureRandom();
/** /**
* Comparator used by {@link #sortBySpecificity(List)}. * Comparator used by {@link #sortBySpecificity(List)}.
*/ */
@ -153,6 +152,9 @@ public abstract class MimeTypeUtils {
*/ */
public static final String TEXT_XML_VALUE = "text/xml"; public static final String TEXT_XML_VALUE = "text/xml";
@Nullable
private static volatile Random random;
static { static {
ALL = MimeType.valueOf(ALL_VALUE); ALL = MimeType.valueOf(ALL_VALUE);
@ -314,15 +316,31 @@ public abstract class MimeTypeUtils {
} }
/**
* Lazily initialize the {@link SecureRandom} for {@link #generateMultipartBoundary()}.
*/
private static Random initRandom() {
Random randomToUse = random;
if (randomToUse == null) {
synchronized (MimeTypeUtils.class) {
randomToUse = random;
if (randomToUse == null) {
randomToUse = new SecureRandom();
random = randomToUse;
}
}
}
return randomToUse;
}
/** /**
* Generate a random MIME boundary as bytes, often used in multipart mime types. * Generate a random MIME boundary as bytes, often used in multipart mime types.
*/ */
public static byte[] generateMultipartBoundary() { public static byte[] generateMultipartBoundary() {
byte[] boundary = new byte[RND.nextInt(11) + 30]; Random randomToUse = initRandom();
byte[] boundary = new byte[randomToUse.nextInt(11) + 30];
for (int i = 0; i < boundary.length; i++) { for (int i = 0; i < boundary.length; i++) {
boundary[i] = BOUNDARY_CHARS[RND.nextInt(BOUNDARY_CHARS.length)]; boundary[i] = BOUNDARY_CHARS[randomToUse.nextInt(BOUNDARY_CHARS.length)];
} }
return boundary; return boundary;
} }

Loading…
Cancel
Save