Skip to content

Commit f0323be

Browse files
committed
Add option to copy HTTP session id to handshake attrs
Issue: SPR-12314
1 parent 4a29e16 commit f0323be

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@
3838
*/
3939
public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor {
4040

41-
private Collection<String> attributeNames;
41+
/**
42+
* The name of the attribute under which the HTTP session id is exposed when
43+
* {@link #setCopyHttpSessionId(boolean) copyHttpSessionId} is "true".
44+
*/
45+
public static final String HTTP_SESSION_ID_ATTR_NAME = "HTTP.SESSION.ID";
46+
47+
private final Collection<String> attributeNames;
48+
49+
private boolean copyHttpSessionId;
4250

4351

4452
/**
@@ -56,6 +64,25 @@ public HttpSessionHandshakeInterceptor(Collection<String> attributeNames) {
5664
this.attributeNames = attributeNames;
5765
}
5866

67+
/**
68+
* When set to "true", the HTTP session id is copied to the WebSocket
69+
* handshake attributes, and is subsequently available via
70+
* {@link org.springframework.web.socket.WebSocketSession#getAttributes()}
71+
* under the key {@link #HTTP_SESSION_ID_ATTR_NAME}.
72+
* <p>By default this is "false".
73+
* @param copyHttpSessionId whether to copy the HTTP session id.
74+
*/
75+
public void setCopyHttpSessionId(boolean copyHttpSessionId) {
76+
this.copyHttpSessionId = copyHttpSessionId;
77+
}
78+
79+
/**
80+
* Whether to copy the HTTP session id to the handshake attributes.
81+
*/
82+
public boolean isCopyHttpSessionId() {
83+
return this.copyHttpSessionId;
84+
}
85+
5986

6087
@Override
6188
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
@@ -72,6 +99,9 @@ public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse res
7299
attributes.put(name, session.getAttribute(name));
73100
}
74101
}
102+
if (isCopyHttpSessionId()) {
103+
attributes.put(HTTP_SESSION_ID_ATTR_NAME, session.getId());
104+
}
75105
}
76106
}
77107
return true;

spring-websocket/src/test/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptorTests.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
import org.junit.Test;
2525
import org.mockito.Mockito;
26+
import org.springframework.mock.web.test.MockHttpSession;
2627
import org.springframework.web.socket.AbstractHttpRequestTests;
2728
import org.springframework.web.socket.WebSocketHandler;
2829

@@ -46,7 +47,7 @@ public void copyAllAttributes() throws Exception {
4647
this.servletRequest.getSession().setAttribute("bar", "baz");
4748

4849
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
49-
interceptor.beforeHandshake(request, response, wsHandler, attributes);
50+
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
5051

5152
assertEquals(2, attributes.size());
5253
assertEquals("bar", attributes.get("foo"));
@@ -64,20 +65,36 @@ public void copySelectedAttributes() throws Exception {
6465

6566
Set<String> names = Collections.singleton("foo");
6667
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(names);
67-
interceptor.beforeHandshake(request, response, wsHandler, attributes);
68+
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
6869

6970
assertEquals(1, attributes.size());
7071
assertEquals("bar", attributes.get("foo"));
7172
}
7273

74+
@Test
75+
public void copyHttpSessionId() throws Exception {
76+
77+
Map<String, Object> attributes = new HashMap<String, Object>();
78+
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
79+
80+
this.servletRequest.setSession(new MockHttpSession(null, "foo"));
81+
82+
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
83+
interceptor.setCopyHttpSessionId(true);
84+
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
85+
86+
assertEquals(1, attributes.size());
87+
assertEquals("foo", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
88+
}
89+
7390
@Test
7491
public void doNotCauseSessionCreation() throws Exception {
7592

7693
Map<String, Object> attributes = new HashMap<String, Object>();
7794
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
7895

7996
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
80-
interceptor.beforeHandshake(request, response, wsHandler, attributes);
97+
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
8198

8299
assertNull(this.servletRequest.getSession(false));
83100
}

0 commit comments

Comments
 (0)