Skip to content

Commit 22ccccd

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

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

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

+10-2
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.
@@ -44,6 +44,7 @@
4444
*
4545
* @author Rob Winch
4646
* @author Vedran Pavic
47+
* @author Yanming Zhou
4748
* @since 1.0
4849
*/
4950
public final class MapSession implements Session, Serializable {
@@ -229,7 +230,14 @@ public int hashCode() {
229230
}
230231

231232
private static String generateId() {
232-
return UUID.randomUUID().toString();
233+
return sessionIdGenerator.generateId();
234+
}
235+
236+
private final static SessionIdGenerator sessionIdGenerator;
237+
238+
static {
239+
List<SessionIdGenerator> generators = SpringFactoriesLoader.loadFactories(SessionIdGenerator.class, null);
240+
sessionIdGenerator = generators.isEmpty() ? SessionIdGenerator.DEFAULT : generators.get(0);
233241
}
234242

235243
private static final long serialVersionUID = 7160779239673823561L;
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+
}
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 getAttributeWhenNullThenNull() {
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.session.SessionIdGenerator=\
2+
org.springframework.session.SessionIdGeneratorTests.MySessionIdGenerator

0 commit comments

Comments
 (0)