Skip to content

Commit b17e7c3

Browse files
committed
Use random id for WebSocket sessions
Issue: SPR-17228
1 parent ee559bb commit b17e7c3

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import org.apache.commons.logging.LogFactory;
2525

2626
import org.springframework.lang.Nullable;
27+
import org.springframework.util.AlternativeJdkIdGenerator;
2728
import org.springframework.util.Assert;
29+
import org.springframework.util.IdGenerator;
2830
import org.springframework.web.socket.BinaryMessage;
2931
import org.springframework.web.socket.CloseStatus;
3032
import org.springframework.web.socket.PingMessage;
@@ -41,8 +43,11 @@
4143
*/
4244
public abstract class AbstractWebSocketSession<T> implements NativeWebSocketSession {
4345

46+
protected static final IdGenerator idGenerator = new AlternativeJdkIdGenerator();
47+
4448
protected static final Log logger = LogFactory.getLog(NativeWebSocketSession.class);
4549

50+
4651
private final Map<String, Object> attributes = new ConcurrentHashMap<>();
4752

4853
@Nullable

spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -34,7 +34,6 @@
3434
import org.springframework.lang.Nullable;
3535
import org.springframework.util.Assert;
3636
import org.springframework.util.CollectionUtils;
37-
import org.springframework.util.ObjectUtils;
3837
import org.springframework.web.socket.BinaryMessage;
3938
import org.springframework.web.socket.CloseStatus;
4039
import org.springframework.web.socket.PingMessage;
@@ -55,8 +54,7 @@
5554
*/
5655
public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
5756

58-
@Nullable
59-
private String id;
57+
private final String id;
6058

6159
@Nullable
6260
private URI uri;
@@ -91,13 +89,13 @@ public JettyWebSocketSession(Map<String, Object> attributes) {
9189
*/
9290
public JettyWebSocketSession(Map<String, Object> attributes, @Nullable Principal user) {
9391
super(attributes);
92+
this.id = idGenerator.generateId().toString();
9493
this.user = user;
9594
}
9695

9796

9897
@Override
9998
public String getId() {
100-
Assert.state(this.id != null, "WebSocket session is not yet initialized");
10199
return this.id;
102100
}
103101

@@ -177,7 +175,6 @@ public boolean isOpen() {
177175
public void initializeNativeSession(Session session) {
178176
super.initializeNativeSession(session);
179177

180-
this.id = ObjectUtils.getIdentityHexString(getNativeSession());
181178
this.uri = session.getUpgradeRequest().getRequestURI();
182179

183180
HttpHeaders headers = new HttpHeaders();

spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -50,8 +50,7 @@
5050
*/
5151
public class StandardWebSocketSession extends AbstractWebSocketSession<Session> {
5252

53-
@Nullable
54-
private String id;
53+
private final String id;
5554

5655
@Nullable
5756
private URI uri;
@@ -102,6 +101,7 @@ public StandardWebSocketSession(@Nullable HttpHeaders headers, @Nullable Map<Str
102101
@Nullable Principal user) {
103102

104103
super(attributes);
104+
this.id = idGenerator.generateId().toString();
105105
headers = (headers != null ? headers : new HttpHeaders());
106106
this.handshakeHeaders = HttpHeaders.readOnlyHttpHeaders(headers);
107107
this.user = user;
@@ -112,7 +112,6 @@ public StandardWebSocketSession(@Nullable HttpHeaders headers, @Nullable Map<Str
112112

113113
@Override
114114
public String getId() {
115-
Assert.state(this.id != null, "WebSocket session is not yet initialized");
116115
return this.id;
117116
}
118117

@@ -189,9 +188,7 @@ public boolean isOpen() {
189188
public void initializeNativeSession(Session session) {
190189
super.initializeNativeSession(session);
191190

192-
this.id = session.getId();
193191
this.uri = session.getRequestURI();
194-
195192
this.acceptedProtocol = session.getNegotiatedSubprotocol();
196193

197194
List<Extension> standardExtensions = getNativeSession().getNegotiatedExtensions();

spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java

Lines changed: 6 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-2018 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.socket.adapter.standard;
1818

19+
import java.net.URI;
1920
import javax.websocket.CloseReason;
2021
import javax.websocket.CloseReason.CloseCodes;
2122
import javax.websocket.MessageHandler;
@@ -56,14 +57,15 @@ public void setup() {
5657

5758
@Test
5859
public void onOpen() throws Throwable {
59-
given(this.session.getId()).willReturn("123");
60+
URI uri = URI.create("http://example.org");
61+
given(this.session.getRequestURI()).willReturn(uri);
6062
this.adapter.onOpen(this.session, null);
6163

6264
verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession);
6365
verify(this.session, atLeast(2)).addMessageHandler(any(MessageHandler.Whole.class));
6466

65-
given(this.session.getId()).willReturn("123");
66-
assertEquals("123", this.webSocketSession.getId());
67+
given(this.session.getRequestURI()).willReturn(uri);
68+
assertEquals(uri, this.webSocketSession.getUri());
6769
}
6870

6971
@Test

0 commit comments

Comments
 (0)