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 ;
@@ -81,14 +83,16 @@ public class MongoIndexedSessionRepository
81
83
82
84
private ApplicationEventPublisher eventPublisher ;
83
85
86
+ private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy .getInstance ();
87
+
84
88
public MongoIndexedSessionRepository (MongoOperations mongoOperations ) {
85
89
this .mongoOperations = mongoOperations ;
86
90
}
87
91
88
92
@ Override
89
93
public MongoSession createSession () {
90
94
91
- MongoSession session = new MongoSession ();
95
+ MongoSession session = new MongoSession (this . sessionIdGenerationStrategy );
92
96
93
97
session .setMaxInactiveInterval (this .defaultMaxInactiveInterval );
94
98
@@ -116,10 +120,13 @@ public MongoSession findById(String id) {
116
120
117
121
MongoSession session = MongoSessionUtils .convertToSession (this .mongoSessionConverter , sessionWrapper );
118
122
119
- if (session != null && session .isExpired ()) {
120
- publishEvent (new SessionExpiredEvent (this , session ));
121
- deleteById (id );
122
- return null ;
123
+ if (session != null ) {
124
+ if (session .isExpired ()) {
125
+ publishEvent (new SessionExpiredEvent (this , session ));
126
+ deleteById (id );
127
+ return null ;
128
+ }
129
+ session .setSessionIdGenerationStrategy (this .sessionIdGenerationStrategy );
123
130
}
124
131
125
132
return session ;
@@ -140,6 +147,7 @@ public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName,
140
147
.map ((query ) -> this .mongoOperations .find (query , Document .class , this .collectionName ))
141
148
.orElse (Collections .emptyList ()).stream ()
142
149
.map ((dbSession ) -> MongoSessionUtils .convertToSession (this .mongoSessionConverter , dbSession ))
150
+ .peek ((session ) -> session .setSessionIdGenerationStrategy (this .sessionIdGenerationStrategy ))
143
151
.collect (Collectors .toMap (MongoSession ::getId , (mapSession ) -> mapSession ));
144
152
}
145
153
@@ -216,4 +224,14 @@ public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSe
216
224
this .mongoSessionConverter = mongoSessionConverter ;
217
225
}
218
226
227
+ /**
228
+ * Set the {@link SessionIdGenerationStrategy} to use to generate session ids.
229
+ * @param sessionIdGenerationStrategy the {@link SessionIdGenerationStrategy} to use
230
+ * @since 3.2
231
+ */
232
+ public void setSessionIdGenerationStrategy (SessionIdGenerationStrategy sessionIdGenerationStrategy ) {
233
+ Assert .notNull (sessionIdGenerationStrategy , "sessionIdGenerationStrategy cannot be null" );
234
+ this .sessionIdGenerationStrategy = sessionIdGenerationStrategy ;
235
+ }
236
+
219
237
}
0 commit comments