Skip to content

Commit 9545081

Browse files
klouds27klouds27
authored andcommitted
Fix import ordering and add end-to-end registry migration test
Signed-off-by: admin <adalwolfad@gmail.com> Signed-off-by: klouds27 <adalwolf@gmail.com>
1 parent 6d9b10d commit 9545081

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

web/src/test/java/org/springframework/security/web/authentication/session/ChangeSessionIdAuthenticationStrategyTests.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323

2424
import org.springframework.context.ApplicationEvent;
2525
import org.springframework.context.ApplicationEventPublisher;
26+
import org.springframework.context.event.SimpleApplicationEventMulticaster;
2627
import org.springframework.mock.web.MockHttpServletRequest;
2728
import org.springframework.mock.web.MockHttpServletResponse;
2829
import org.springframework.security.core.Authentication;
2930
import org.springframework.security.core.session.SessionIdChangedEvent;
31+
import org.springframework.security.core.session.SessionRegistryImpl;
3032

3133
import static org.assertj.core.api.Assertions.assertThat;
3234
import static org.mockito.Mockito.mock;
@@ -48,7 +50,30 @@ public void applySessionFixation() {
4850
}
4951

5052
@Test
51-
public void onAuthenticationPublishesSessionIdChangedEventWithoutHttpSessionEventPublisher() {
53+
public void onAuthenticationWhenRegistryHasOldSessionThenMigratesWithoutHttpSessionEventPublisher() {
54+
// Reproduces gh-19007: without HttpSessionEventPublisher the old session ID used
55+
// to remain as a ghost entry in the registry after session fixation rotation,
56+
// causing ConcurrentSessionControlAuthenticationStrategy to count an extra session.
57+
SessionRegistryImpl registry = new SessionRegistryImpl();
58+
SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
59+
multicaster.addApplicationListener(registry);
60+
ChangeSessionIdAuthenticationStrategy strategy = new ChangeSessionIdAuthenticationStrategy();
61+
strategy.setApplicationEventPublisher((event) -> {
62+
if (event instanceof ApplicationEvent applicationEvent) {
63+
multicaster.multicastEvent(applicationEvent);
64+
}
65+
});
66+
MockHttpServletRequest request = new MockHttpServletRequest();
67+
Object principal = "testPrincipal";
68+
registry.registerNewSession(request.getSession().getId(), principal);
69+
strategy.onAuthentication(mock(Authentication.class), request, new MockHttpServletResponse());
70+
String newSessionId = request.getSession().getId();
71+
assertThat(registry.getSessionInformation(newSessionId)).isNotNull();
72+
assertThat(registry.getAllSessions(principal, false)).hasSize(1);
73+
}
74+
75+
@Test
76+
public void onAuthenticationPublishesSessionFixationAndSessionIdChangedEvents() {
5277
ChangeSessionIdAuthenticationStrategy strategy = new ChangeSessionIdAuthenticationStrategy();
5378
MockHttpServletRequest request = new MockHttpServletRequest();
5479
String oldSessionId = request.getSession().getId();
@@ -58,9 +83,12 @@ public void onAuthenticationPublishesSessionIdChangedEventWithoutHttpSessionEven
5883
ArgumentCaptor<ApplicationEvent> captor = ArgumentCaptor.forClass(ApplicationEvent.class);
5984
verify(eventPublisher, times(2)).publishEvent(captor.capture());
6085
List<ApplicationEvent> events = captor.getAllValues();
61-
assertThat(events.get(0)).isInstanceOf(SessionFixationProtectionEvent.class);
62-
assertThat(events.get(1)).isInstanceOf(SessionIdChangedEvent.class);
63-
SessionIdChangedEvent idChangedEvent = (SessionIdChangedEvent) events.get(1);
86+
assertThat(events).hasAtLeastOneElementOfType(SessionFixationProtectionEvent.class);
87+
SessionIdChangedEvent idChangedEvent = events.stream()
88+
.filter(SessionIdChangedEvent.class::isInstance)
89+
.map(SessionIdChangedEvent.class::cast)
90+
.findFirst()
91+
.orElseThrow();
6492
assertThat(idChangedEvent.getOldSessionId()).isEqualTo(oldSessionId);
6593
assertThat(idChangedEvent.getNewSessionId()).isEqualTo(request.getSession().getId());
6694
assertThat(idChangedEvent.getNewSessionId()).isNotEqualTo(oldSessionId);

web/src/test/java/org/springframework/security/web/session/DefaultSessionAuthenticationStrategyTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
import org.springframework.mock.web.MockHttpServletRequest;
2727
import org.springframework.mock.web.MockHttpServletResponse;
2828
import org.springframework.security.core.Authentication;
29+
import org.springframework.security.core.session.SessionIdChangedEvent;
2930
import org.springframework.security.web.authentication.session.SessionFixationProtectionEvent;
3031
import org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy;
3132

32-
import org.springframework.security.core.session.SessionIdChangedEvent;
33-
3433
import static org.assertj.core.api.Assertions.assertThat;
3534
import static org.mockito.Mockito.mock;
3635
import static org.mockito.Mockito.times;

0 commit comments

Comments
 (0)