Skip to content

Commit 439cad6

Browse files
committed
Provide Session Id Generation Strategy
Fix spring-projectsGH-11
1 parent 1e10dfe commit 439cad6

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

spring-session-core/src/main/java/org/springframework/session/MapSession.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,9 +21,10 @@
2121
import java.time.Instant;
2222
import java.util.HashMap;
2323
import java.util.HashSet;
24+
import java.util.Iterator;
2425
import java.util.Map;
26+
import java.util.ServiceLoader;
2527
import java.util.Set;
26-
import java.util.UUID;
2728

2829
/**
2930
* <p>
@@ -44,6 +45,7 @@
4445
*
4546
* @author Rob Winch
4647
* @author Vedran Pavic
48+
* @author Yanming Zhou
4749
* @since 1.0
4850
*/
4951
public final class MapSession implements Session, Serializable {
@@ -229,7 +231,14 @@ public int hashCode() {
229231
}
230232

231233
private static String generateId() {
232-
return UUID.randomUUID().toString();
234+
return sessionIdGenerator.generateId();
235+
}
236+
237+
private static final SessionIdGenerator sessionIdGenerator;
238+
239+
static {
240+
Iterator<SessionIdGenerator> generators = ServiceLoader.load(SessionIdGenerator.class).iterator();
241+
sessionIdGenerator = generators.hasNext() ? generators.next() : SessionIdGenerator.DEFAULT;
233242
}
234243

235244
private static final long serialVersionUID = 7160779239673823561L;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2014-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session;
18+
19+
import java.util.UUID;
20+
21+
/**
22+
* <p>
23+
* Id generator for creating new session.
24+
* </p>
25+
*
26+
* @author Yanming Zhou
27+
*/
28+
@FunctionalInterface
29+
public interface SessionIdGenerator {
30+
31+
/**
32+
* Using random UUID as default id generator.
33+
*/
34+
SessionIdGenerator DEFAULT = () -> UUID.randomUUID().toString();
35+
36+
/**
37+
* Generate and return a new session identifier.
38+
* @return the newly generated session id
39+
*/
40+
String generateId();
41+
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2014-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session;
18+
19+
import java.util.concurrent.atomic.AtomicLong;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* @author Yanming Zhou
27+
*/
28+
class SessionIdGeneratorTests {
29+
30+
static final String prefix = "sessionid-";
31+
32+
@Test
33+
void sessionIdShouldStartsWithCustomizedPrefix() {
34+
MapSession session = new MapSession();
35+
assertThat(session.getId()).startsWith(prefix);
36+
session.changeSessionId();
37+
assertThat(session.getId()).startsWith(prefix);
38+
}
39+
40+
public static class MySessionIdGenerator implements SessionIdGenerator {
41+
42+
private final AtomicLong counter = new AtomicLong();
43+
44+
@Override
45+
public String generateId() {
46+
return prefix + this.counter.incrementAndGet();
47+
}
48+
49+
}
50+
51+
}

0 commit comments

Comments
 (0)