Skip to content

Commit 5fc6414

Browse files
aj-jaswantheleftherias
authored andcommitted
SessionRegistryImpl is now aware of SessionIdChangedEvent
1 parent ae532c0 commit 5fc6414

File tree

7 files changed

+164
-5
lines changed

7 files changed

+164
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.core.session;
17+
18+
import org.springframework.context.ApplicationEvent;
19+
20+
public abstract class SessionIdChangedEvent extends ApplicationEvent {
21+
22+
public SessionIdChangedEvent(Object source) {
23+
super(source);
24+
}
25+
26+
public abstract String getOldSessionId();
27+
28+
public abstract String getNewSessionId();
29+
}

core/src/main/java/org/springframework/security/core/session/SessionRegistryImpl.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
21+
import org.springframework.context.ApplicationEvent;
2122
import org.springframework.context.ApplicationListener;
2223
import org.springframework.util.Assert;
2324

@@ -40,7 +41,7 @@
4041
* @author Luke Taylor
4142
*/
4243
public class SessionRegistryImpl implements SessionRegistry,
43-
ApplicationListener<SessionDestroyedEvent> {
44+
ApplicationListener<ApplicationEvent> {
4445

4546
// ~ Instance fields
4647
// ================================================================================================
@@ -101,9 +102,18 @@ public SessionInformation getSessionInformation(String sessionId) {
101102
return sessionIds.get(sessionId);
102103
}
103104

104-
public void onApplicationEvent(SessionDestroyedEvent event) {
105-
String sessionId = event.getId();
106-
removeSessionInformation(sessionId);
105+
public void onApplicationEvent(ApplicationEvent event) {
106+
if (event instanceof SessionDestroyedEvent) {
107+
SessionDestroyedEvent sessionDestroyedEvent = (SessionDestroyedEvent) event;
108+
String sessionId = sessionDestroyedEvent.getId();
109+
removeSessionInformation(sessionId);
110+
} else if (event instanceof SessionIdChangedEvent) {
111+
SessionIdChangedEvent sessionIdChangedEvent = (SessionIdChangedEvent) event;
112+
String oldSessionId = sessionIdChangedEvent.getOldSessionId();
113+
Object principal = sessionIds.get(oldSessionId).getPrincipal();
114+
removeSessionInformation(oldSessionId);
115+
registerNewSession(sessionIdChangedEvent.getNewSessionId(), principal);
116+
}
107117
}
108118

109119
public void refreshLastRequest(String sessionId) {

core/src/test/java/org/springframework/security/core/session/SessionRegistryImplTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,33 @@ public List<SecurityContext> getSecurityContexts() {
6969
assertThat(sessionRegistry.getSessionInformation(sessionId)).isNull();
7070
}
7171

72+
@Test
73+
public void sessionIdChangedEventRemovesOldSessionAndAddsANewSession() {
74+
Object principal = "Some principal object";
75+
final String sessionId = "zzzz";
76+
final String newSessionId = "123";
77+
78+
// Register new Session
79+
sessionRegistry.registerNewSession(sessionId, principal);
80+
81+
// De-register session via an ApplicationEvent
82+
sessionRegistry.onApplicationEvent(new SessionIdChangedEvent("") {
83+
@Override
84+
public String getOldSessionId() {
85+
return sessionId;
86+
}
87+
88+
@Override
89+
public String getNewSessionId() {
90+
return newSessionId;
91+
}
92+
});
93+
94+
assertThat(sessionRegistry.getSessionInformation(sessionId)).isNull();
95+
assertThat(sessionRegistry.getSessionInformation(newSessionId)).isNotNull();
96+
assertThat(sessionRegistry.getSessionInformation(newSessionId).getPrincipal()).isEqualTo(principal);
97+
}
98+
7299
@Test
73100
public void testMultiplePrincipals() {
74101
Object principal1 = "principal_1";

web/src/main/java/org/springframework/security/web/session/HttpSessionEventPublisher.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import javax.servlet.ServletContext;
2727
import javax.servlet.http.HttpSessionEvent;
28+
import javax.servlet.http.HttpSessionIdListener;
2829
import javax.servlet.http.HttpSessionListener;
2930

3031
/**
@@ -44,7 +45,7 @@
4445
*
4546
* @author Ray Krueger
4647
*/
47-
public class HttpSessionEventPublisher implements HttpSessionListener {
48+
public class HttpSessionEventPublisher implements HttpSessionListener, HttpSessionIdListener {
4849
// ~ Static fields/initializers
4950
// =====================================================================================
5051

@@ -90,4 +91,16 @@ public void sessionDestroyed(HttpSessionEvent event) {
9091

9192
getContext(event.getSession().getServletContext()).publishEvent(e);
9293
}
94+
95+
@Override
96+
public void sessionIdChanged(HttpSessionEvent event, String oldSessionId) {
97+
HttpSessionIdChangedEvent e = new HttpSessionIdChangedEvent(event.getSession(), oldSessionId);
98+
Log log = LogFactory.getLog(LOGGER_NAME);
99+
100+
if (log.isDebugEnabled()) {
101+
log.debug("Publishing event: " + e);
102+
}
103+
104+
getContext(event.getSession().getServletContext()).publishEvent(e);
105+
}
93106
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.session;
18+
19+
import javax.servlet.http.HttpSession;
20+
21+
import org.springframework.security.core.session.SessionIdChangedEvent;
22+
23+
/**
24+
* Published by the {@link HttpSessionEventPublisher} when an {@code HttpSession} id
25+
* is changed
26+
*
27+
*/
28+
public class HttpSessionIdChangedEvent extends SessionIdChangedEvent {
29+
private final String oldSessionId;
30+
private final String newSessionid;
31+
// ~ Constructors
32+
// ===================================================================================================
33+
34+
public HttpSessionIdChangedEvent(HttpSession session, String oldSessionId) {
35+
super(session);
36+
this.oldSessionId = oldSessionId;
37+
this.newSessionid = session.getId();
38+
}
39+
40+
public String getOldSessionId() {
41+
return oldSessionId;
42+
}
43+
44+
@Override
45+
public String getNewSessionId() {
46+
return newSessionid;
47+
}
48+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public void publishedEventIsReceivedbyListener() {
7272
assertThat(listener.getDestroyedEvent()).isNotNull();
7373
assertThat(listener.getCreatedEvent()).isNull();
7474
assertThat(listener.getDestroyedEvent().getSession()).isEqualTo(session);
75+
76+
publisher.sessionIdChanged(event, "oldSessionId");
77+
assertThat(listener.getSessionIdChangedEvent()).isNotNull();
78+
assertThat(listener.getSessionIdChangedEvent().getOldSessionId()).isEqualTo("oldSessionId");
79+
listener.setSessionIdChangedEvent(null);
7580
}
7681

7782
@Test
@@ -108,6 +113,11 @@ public void publishedEventIsReceivedbyListenerChildContext() {
108113
assertThat(listener.getDestroyedEvent()).isNotNull();
109114
assertThat(listener.getCreatedEvent()).isNull();
110115
assertThat(listener.getDestroyedEvent().getSession()).isEqualTo(session);
116+
117+
publisher.sessionIdChanged(event, "oldSessionId");
118+
assertThat(listener.getSessionIdChangedEvent()).isNotNull();
119+
assertThat(listener.getSessionIdChangedEvent().getOldSessionId()).isEqualTo("oldSessionId");
120+
listener.setSessionIdChangedEvent(null);
111121
}
112122

113123
// SEC-2599
@@ -131,4 +141,14 @@ public void sessionDestroyedNullApplicationContext() {
131141

132142
publisher.sessionDestroyed(event);
133143
}
144+
145+
@Test(expected = IllegalStateException.class)
146+
public void sessionIdChangeNullApplicationContext() {
147+
HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
148+
MockServletContext servletContext = new MockServletContext();
149+
MockHttpSession session = new MockHttpSession(servletContext);
150+
HttpSessionEvent event = new HttpSessionEvent(session);
151+
152+
publisher.sessionIdChanged(event, "oldSessionId");
153+
}
134154
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class MockApplicationListener implements ApplicationListener<ApplicationE
3232

3333
private HttpSessionCreatedEvent createdEvent;
3434
private HttpSessionDestroyedEvent destroyedEvent;
35+
private HttpSessionIdChangedEvent sessionIdChangedEvent;
3536

3637
// ~ Methods
3738
// ========================================================================================================
@@ -51,6 +52,9 @@ public void onApplicationEvent(ApplicationEvent event) {
5152
else if (event instanceof HttpSessionDestroyedEvent) {
5253
destroyedEvent = (HttpSessionDestroyedEvent) event;
5354
}
55+
else if (event instanceof HttpSessionIdChangedEvent) {
56+
sessionIdChangedEvent = (HttpSessionIdChangedEvent) event;
57+
}
5458
}
5559

5660
public void setCreatedEvent(HttpSessionCreatedEvent createdEvent) {
@@ -60,4 +64,12 @@ public void setCreatedEvent(HttpSessionCreatedEvent createdEvent) {
6064
public void setDestroyedEvent(HttpSessionDestroyedEvent destroyedEvent) {
6165
this.destroyedEvent = destroyedEvent;
6266
}
67+
68+
public void setSessionIdChangedEvent(HttpSessionIdChangedEvent sessionIdChangedEvent) {
69+
this.sessionIdChangedEvent = sessionIdChangedEvent;
70+
}
71+
72+
public HttpSessionIdChangedEvent getSessionIdChangedEvent() {
73+
return sessionIdChangedEvent;
74+
}
6375
}

0 commit comments

Comments
 (0)