@@ -45,7 +45,7 @@ public class InMemoryWebSessionStore implements WebSessionStore {
4545
4646	private  Clock  clock  = Clock .system (ZoneId .of ("GMT" ));
4747
48- 	private  final  Map <String , WebSession > sessions  = new  ConcurrentHashMap <>();
48+ 	private  final  Map <String , InMemoryWebSession > sessions  = new  ConcurrentHashMap <>();
4949
5050
5151	/** 
@@ -77,7 +77,7 @@ public Mono<WebSession> createWebSession() {
7777
7878	@ Override 
7979	public  Mono <WebSession > retrieveSession (String  id ) {
80- 		WebSession  session  = this .sessions .get (id );
80+ 		InMemoryWebSession  session  = this .sessions .get (id );
8181		if  (session  == null ) {
8282			return  Mono .empty ();
8383		}
@@ -86,6 +86,7 @@ else if (session.isExpired()) {
8686			return  Mono .empty ();
8787		}
8888		else  {
89+ 			session .updateLastAccessTime ();
8990			return  Mono .just (session );
9091		}
9192	}
@@ -98,27 +99,14 @@ public Mono<Void> removeSession(String id) {
9899
99100	public  Mono <WebSession > updateLastAccessTime (WebSession  webSession ) {
100101		return  Mono .fromSupplier (() -> {
102+ 			Assert .isInstanceOf (InMemoryWebSession .class , webSession );
101103			InMemoryWebSession  session  = (InMemoryWebSession ) webSession ;
102- 			Instant   lastAccessTime  =  Instant . now ( getClock () );
103- 			return  new   InMemoryWebSession ( session ,  lastAccessTime ) ;
104+ 			session . updateLastAccessTime ( );
105+ 			return  session ;
104106		});
105107	}
106108
107109
108- 	// Private methods for InMemoryWebSession 
109- 
110- 	private  Mono <Void > changeSessionId (String  oldId , WebSession  session ) {
111- 		this .sessions .remove (oldId );
112- 		this .sessions .put (session .getId (), session );
113- 		return  Mono .empty ();
114- 	}
115- 
116- 	private  Mono <Void > storeSession (WebSession  session ) {
117- 		this .sessions .put (session .getId (), session );
118- 		return  Mono .empty ();
119- 	}
120- 
121- 
122110	private  class  InMemoryWebSession  implements  WebSession  {
123111
124112		private  final  AtomicReference <String > id ;
@@ -127,12 +115,13 @@ private class InMemoryWebSession implements WebSession {
127115
128116		private  final  Instant  creationTime ;
129117
130- 		private  final  Instant  lastAccessTime ;
118+ 		private  volatile  Instant  lastAccessTime ;
131119
132120		private  volatile  Duration  maxIdleTime ;
133121
134122		private  volatile  boolean  started ;
135123
124+ 
136125		InMemoryWebSession () {
137126			this .id  = new  AtomicReference <>(String .valueOf (idGenerator .generateId ()));
138127			this .attributes  = new  ConcurrentHashMap <>();
@@ -141,14 +130,6 @@ private class InMemoryWebSession implements WebSession {
141130			this .maxIdleTime  = Duration .ofMinutes (30 );
142131		}
143132
144- 		InMemoryWebSession (InMemoryWebSession  existingSession , Instant  lastAccessTime ) {
145- 			this .id  = existingSession .id ;
146- 			this .attributes  = existingSession .attributes ;
147- 			this .creationTime  = existingSession .creationTime ;
148- 			this .lastAccessTime  = lastAccessTime ;
149- 			this .maxIdleTime  = existingSession .maxIdleTime ;
150- 			this .started  = existingSession .isStarted (); // Use method (explicit or implicit start) 
151- 		}
152133
153134		@ Override 
154135		public  String  getId () {
@@ -192,22 +173,33 @@ public boolean isStarted() {
192173
193174		@ Override 
194175		public  Mono <Void > changeSessionId () {
195- 			String  oldId  = this .id .get ();
176+ 			String  currentId  = this .id .get ();
177+ 			if  (InMemoryWebSessionStore .this .sessions .remove (currentId ) == null ) {
178+ 				return  Mono .error (new  IllegalStateException (
179+ 						"Failed to change session id: "  + currentId  +
180+ 								" because the Session is no longer present in the store." ));
181+ 			}
196182			String  newId  = String .valueOf (idGenerator .generateId ());
197183			this .id .set (newId );
198- 			return  InMemoryWebSessionStore .this .changeSessionId (oldId , this ).doOnError (ex  -> this .id .set (oldId ));
184+ 			InMemoryWebSessionStore .this .sessions .put (this .getId (), this );
185+ 			return  Mono .empty ();
199186		}
200187
201188		@ Override 
202189		public  Mono <Void > save () {
203- 			return  InMemoryWebSessionStore .this .storeSession (this );
190+ 			InMemoryWebSessionStore .this .sessions .put (this .getId (), this );
191+ 			return  Mono .empty ();
204192		}
205193
206194		@ Override 
207195		public  boolean  isExpired () {
208196			return  (isStarted () && !this .maxIdleTime .isNegative () &&
209197					Instant .now (getClock ()).minus (this .maxIdleTime ).isAfter (this .lastAccessTime ));
210198		}
199+ 
200+ 		private  void  updateLastAccessTime () {
201+ 			this .lastAccessTime  = Instant .now (getClock ());
202+ 		}
211203	}
212204
213205}
0 commit comments