Skip to content

Commit

Permalink
Prevent NullPointerException when session ID changes
Browse files Browse the repository at this point in the history
The old session ID may not exist in the session registry if the user is not authenticated.

Closes spring-projectsgh-9011
  • Loading branch information
eleftherias committed Sep 18, 2020
1 parent 6e6d382 commit a5b97bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ public void onApplicationEvent(AbstractSessionEvent event) {
else if (event instanceof SessionIdChangedEvent) {
SessionIdChangedEvent sessionIdChangedEvent = (SessionIdChangedEvent) event;
String oldSessionId = sessionIdChangedEvent.getOldSessionId();
Object principal = this.sessionIds.get(oldSessionId).getPrincipal();
removeSessionInformation(oldSessionId);
registerNewSession(sessionIdChangedEvent.getNewSessionId(), principal);
if (this.sessionIds.containsKey(oldSessionId)) {
Object principal = this.sessionIds.get(oldSessionId).getPrincipal();
removeSessionInformation(oldSessionId);
registerNewSession(sessionIdChangedEvent.getNewSessionId(), principal);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ public void testTwoSessionsOnePrincipalHandling() {
assertThat(this.sessionRegistry.getAllSessions(principal, false)).isEmpty();
}

@Test
public void sessionIdChangedEventWhenSessionIdNotSavedThenDoesNothing() {
final String oldSessionId = "old-session-id";
final String newSessionId = "new-session-id";
this.sessionRegistry.onApplicationEvent(new SessionIdChangedEvent("") {
@Override
public String getOldSessionId() {
return oldSessionId;
}

@Override
public String getNewSessionId() {
return newSessionId;
}
});
assertThat(this.sessionRegistry.getSessionInformation(oldSessionId)).isNull();
assertThat(this.sessionRegistry.getSessionInformation(newSessionId)).isNull();
}

private boolean contains(String sessionId, Object principal) {
List<SessionInformation> info = this.sessionRegistry.getAllSessions(principal, false);
for (SessionInformation sessionInformation : info) {
Expand Down

0 comments on commit a5b97bb

Please sign in to comment.