36
36
import org .springframework .lang .Nullable ;
37
37
import org .springframework .session .FindByIndexNameSessionRepository ;
38
38
import org .springframework .session .MapSession ;
39
+ import org .springframework .session .SessionIdGenerationStrategy ;
40
+ import org .springframework .session .UuidSessionIdGenerationStrategy ;
39
41
import org .springframework .session .events .SessionCreatedEvent ;
40
42
import org .springframework .session .events .SessionDeletedEvent ;
41
43
import org .springframework .session .events .SessionExpiredEvent ;
@@ -70,6 +72,8 @@ public class MongoIndexedSessionRepository
70
72
71
73
private static final Log logger = LogFactory .getLog (MongoIndexedSessionRepository .class );
72
74
75
+ private static final SessionIdGenerationStrategy DEFAULT_STRATEGY = UuidSessionIdGenerationStrategy .getInstance ();
76
+
73
77
private final MongoOperations mongoOperations ;
74
78
75
79
private Duration defaultMaxInactiveInterval = Duration .ofSeconds (MapSession .DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS );
@@ -81,25 +85,36 @@ public class MongoIndexedSessionRepository
81
85
82
86
private ApplicationEventPublisher eventPublisher ;
83
87
88
+ private SessionIdGenerationStrategy sessionIdGenerationStrategy = DEFAULT_STRATEGY ;
89
+
84
90
public MongoIndexedSessionRepository (MongoOperations mongoOperations ) {
85
91
this .mongoOperations = mongoOperations ;
86
92
}
87
93
88
94
@ Override
89
95
public MongoSession createSession () {
90
96
91
- MongoSession session = new MongoSession ();
97
+ String sessionId = this .sessionIdGenerationStrategy .generate ();
98
+ MongoSession session = new MongoSession (sessionId );
92
99
93
100
session .setMaxInactiveInterval (this .defaultMaxInactiveInterval );
94
101
95
102
publishEvent (new SessionCreatedEvent (this , session ));
96
103
97
- return session ;
104
+ return wrapSession (session );
105
+ }
106
+
107
+ private SessionIdGenerationStrategyAwareMongoSession wrapSession (MongoSession session ) {
108
+ return new SessionIdGenerationStrategyAwareMongoSession (session , this .sessionIdGenerationStrategy );
98
109
}
99
110
100
111
@ Override
101
112
public void save (MongoSession session ) {
102
- DBObject dbObject = MongoSessionUtils .convertToDBObject (this .mongoSessionConverter , session );
113
+ MongoSession mongoSession = session ;
114
+ if (session instanceof SessionIdGenerationStrategyAwareMongoSession awareMongoSession ) {
115
+ mongoSession = awareMongoSession .getDelegate ();
116
+ }
117
+ DBObject dbObject = MongoSessionUtils .convertToDBObject (this .mongoSessionConverter , mongoSession );
103
118
Assert .notNull (dbObject , "dbObject must not be null" );
104
119
this .mongoOperations .save (dbObject , this .collectionName );
105
120
}
@@ -116,10 +131,13 @@ public MongoSession findById(String id) {
116
131
117
132
MongoSession session = MongoSessionUtils .convertToSession (this .mongoSessionConverter , sessionWrapper );
118
133
119
- if (session != null && session .isExpired ()) {
120
- publishEvent (new SessionExpiredEvent (this , session ));
121
- deleteById (id );
122
- return null ;
134
+ if (session != null ) {
135
+ if (session .isExpired ()) {
136
+ publishEvent (new SessionExpiredEvent (this , session ));
137
+ deleteById (id );
138
+ return null ;
139
+ }
140
+ return wrapSession (session );
123
141
}
124
142
125
143
return session ;
@@ -140,7 +158,7 @@ public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName,
140
158
.map ((query ) -> this .mongoOperations .find (query , Document .class , this .collectionName ))
141
159
.orElse (Collections .emptyList ()).stream ()
142
160
.map ((dbSession ) -> MongoSessionUtils .convertToSession (this .mongoSessionConverter , dbSession ))
143
- .collect (Collectors .toMap (MongoSession ::getId , (mapSession ) -> mapSession ));
161
+ .map ( this :: wrapSession ). collect (Collectors .toMap (MongoSession ::getId , (mapSession ) -> mapSession ));
144
162
}
145
163
146
164
@ Override
@@ -216,4 +234,9 @@ public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSe
216
234
this .mongoSessionConverter = mongoSessionConverter ;
217
235
}
218
236
237
+ public void setSessionIdGenerationStrategy (SessionIdGenerationStrategy sessionIdGenerationStrategy ) {
238
+ Assert .notNull (sessionIdGenerationStrategy , "sessionIdGenerationStrategy cannot be null" );
239
+ this .sessionIdGenerationStrategy = sessionIdGenerationStrategy ;
240
+ }
241
+
219
242
}
0 commit comments