@@ -72,8 +72,6 @@ public class MongoIndexedSessionRepository
72
72
73
73
private static final Log logger = LogFactory .getLog (MongoIndexedSessionRepository .class );
74
74
75
- private static final SessionIdGenerationStrategy DEFAULT_STRATEGY = UuidSessionIdGenerationStrategy .getInstance ();
76
-
77
75
private final MongoOperations mongoOperations ;
78
76
79
77
private Duration defaultMaxInactiveInterval = Duration .ofSeconds (MapSession .DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS );
@@ -85,7 +83,7 @@ public class MongoIndexedSessionRepository
85
83
86
84
private ApplicationEventPublisher eventPublisher ;
87
85
88
- private SessionIdGenerationStrategy sessionIdGenerationStrategy = DEFAULT_STRATEGY ;
86
+ private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy . getInstance () ;
89
87
90
88
public MongoIndexedSessionRepository (MongoOperations mongoOperations ) {
91
89
this .mongoOperations = mongoOperations ;
@@ -94,27 +92,18 @@ public MongoIndexedSessionRepository(MongoOperations mongoOperations) {
94
92
@ Override
95
93
public MongoSession createSession () {
96
94
97
- String sessionId = this .sessionIdGenerationStrategy .generate ();
98
- MongoSession session = new MongoSession (sessionId );
95
+ MongoSession session = new MongoSession (this .sessionIdGenerationStrategy );
99
96
100
97
session .setMaxInactiveInterval (this .defaultMaxInactiveInterval );
101
98
102
99
publishEvent (new SessionCreatedEvent (this , session ));
103
100
104
- return wrapSession (session );
105
- }
106
-
107
- private SessionIdGenerationStrategyAwareMongoSession wrapSession (MongoSession session ) {
108
- return new SessionIdGenerationStrategyAwareMongoSession (session , this .sessionIdGenerationStrategy );
101
+ return session ;
109
102
}
110
103
111
104
@ Override
112
105
public void save (MongoSession session ) {
113
- MongoSession mongoSession = session ;
114
- if (session instanceof SessionIdGenerationStrategyAwareMongoSession awareMongoSession ) {
115
- mongoSession = awareMongoSession .getDelegate ();
116
- }
117
- DBObject dbObject = MongoSessionUtils .convertToDBObject (this .mongoSessionConverter , mongoSession );
106
+ DBObject dbObject = MongoSessionUtils .convertToDBObject (this .mongoSessionConverter , session );
118
107
Assert .notNull (dbObject , "dbObject must not be null" );
119
108
this .mongoOperations .save (dbObject , this .collectionName );
120
109
}
@@ -137,7 +126,7 @@ public MongoSession findById(String id) {
137
126
deleteById (id );
138
127
return null ;
139
128
}
140
- return wrapSession ( session );
129
+ session . setSessionIdGenerationStrategy ( this . sessionIdGenerationStrategy );
141
130
}
142
131
143
132
return session ;
@@ -158,7 +147,8 @@ public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName,
158
147
.map ((query ) -> this .mongoOperations .find (query , Document .class , this .collectionName ))
159
148
.orElse (Collections .emptyList ()).stream ()
160
149
.map ((dbSession ) -> MongoSessionUtils .convertToSession (this .mongoSessionConverter , dbSession ))
161
- .map (this ::wrapSession ).collect (Collectors .toMap (MongoSession ::getId , (mapSession ) -> mapSession ));
150
+ .peek ((session ) -> session .setSessionIdGenerationStrategy (this .sessionIdGenerationStrategy ))
151
+ .collect (Collectors .toMap (MongoSession ::getId , (mapSession ) -> mapSession ));
162
152
}
163
153
164
154
@ Override
@@ -234,6 +224,11 @@ public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSe
234
224
this .mongoSessionConverter = mongoSessionConverter ;
235
225
}
236
226
227
+ /**
228
+ * Set the {@link SessionIdGenerationStrategy} to use to generate session ids.
229
+ * @param sessionIdGenerationStrategy the {@link SessionIdGenerationStrategy} to use
230
+ * @since 3.1
231
+ */
237
232
public void setSessionIdGenerationStrategy (SessionIdGenerationStrategy sessionIdGenerationStrategy ) {
238
233
Assert .notNull (sessionIdGenerationStrategy , "sessionIdGenerationStrategy cannot be null" );
239
234
this .sessionIdGenerationStrategy = sessionIdGenerationStrategy ;
0 commit comments