Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 025e2cd

Browse files
committed
better sessionId generator fix jooby-project#339
1 parent 22c9fd8 commit 025e2cd

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

jooby/src/main/java/org/jooby/Session.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
import static java.util.Objects.requireNonNull;
2222

23+
import java.security.SecureRandom;
2324
import java.util.Map;
2425
import java.util.Optional;
25-
import java.util.UUID;
2626
import java.util.concurrent.ConcurrentHashMap;
2727
import java.util.concurrent.ConcurrentMap;
2828

29+
import com.google.common.io.BaseEncoding;
30+
2931
/**
3032
* <p>
3133
* Sessions are created on demand via: {@link Request#session()}.
@@ -185,6 +187,9 @@ public Cookie.Definition cookie() {
185187
*/
186188
interface Store {
187189

190+
/** Single secure random instance. */
191+
SecureRandom rnd = new SecureRandom();
192+
188193
/**
189194
* Get a session by ID (if any).
190195
*
@@ -210,14 +215,14 @@ interface Store {
210215
void delete(String id);
211216

212217
/**
213-
* Generate a session ID, default algorithm use an {@link UUID}.
218+
* Generate a session ID.
214219
*
215220
* @return A unique session ID.
216221
*/
217222
default String generateID() {
218-
UUID uuid = UUID.randomUUID();
219-
return Long.toString(Math.abs(uuid.getMostSignificantBits()), 36)
220-
+ Long.toString(Math.abs(uuid.getLeastSignificantBits()), 36);
223+
byte[] bytes = new byte[30];
224+
rnd.nextBytes(bytes);
225+
return BaseEncoding.base64Url().encode(bytes);
221226
}
222227
}
223228

0 commit comments

Comments
 (0)