Skip to content

Commit 270aba2

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

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed

spring-session-core/spring-session-core.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apply plugin: 'io.spring.convention.spring-module'
33
description = "Spring Session"
44

55
dependencies {
6+
api "org.springframework:spring-core"
67
api "org.springframework:spring-jcl"
78

89
optional "io.projectreactor:reactor-core"

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

+13-3
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,11 @@
2121
import java.time.Instant;
2222
import java.util.HashMap;
2323
import java.util.HashSet;
24+
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Set;
26-
import java.util.UUID;
27+
28+
import org.springframework.core.io.support.SpringFactoriesLoader;
2729

2830
/**
2931
* <p>
@@ -44,6 +46,7 @@
4446
*
4547
* @author Rob Winch
4648
* @author Vedran Pavic
49+
* @author Yanming Zhou
4750
* @since 1.0
4851
*/
4952
public final class MapSession implements Session, Serializable {
@@ -229,7 +232,14 @@ public int hashCode() {
229232
}
230233

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

235245
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)