-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/org/owasp/webgoat/lessons/hijacksession/mytest/SessionIdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.owasp.webgoat.lessons.hijacksession.mytest; | ||
|
||
import java.time.Instant; | ||
import java.util.Random; | ||
import java.util.function.Supplier; | ||
|
||
public class SessionIdGenerator { | ||
// private static int id = 1000; | ||
private static long id = new Random().nextLong() & Long.MAX_VALUE; | ||
/** | ||
* 用于生成 SessionID 的方法 | ||
* | ||
* <p>() -> ++id + "-" + Instant.now().toEpochMilli(): 这是一个 lambda 表达式,用于实现 Supplier 接口的 get 方法。 | ||
* Instant.now().toEpochMilli() 返回当前时间的纪元毫秒数(自 1970-01-01T00:00:00Z 开始的毫秒数)。 | ||
*/ | ||
private static final Supplier<String> GENERATE_SESSION_ID = | ||
() -> ++id + "-" + Instant.now().toEpochMilli(); | ||
|
||
public static String generateSessionId() { | ||
return GENERATE_SESSION_ID.get(); | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
for (int i = 0; i < 20; i++) { | ||
System.out.println(generateSessionId()); | ||
Thread.sleep(1); | ||
} | ||
} | ||
} |