From 3302798e2ff515205183f9a26a2aca8d92cc5352 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 5 Sep 2018 19:48:28 -0400 Subject: [PATCH] Use random id for WebSocket sessions Issue: SPR-17228 --- .../web/socket/adapter/AbstractWebSocketSession.java | 5 +++++ .../socket/adapter/jetty/JettyWebSocketSession.java | 9 +++------ .../adapter/standard/StandardWebSocketSession.java | 9 +++------ .../standard/StandardWebSocketHandlerAdapterTests.java | 10 ++++++---- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java index 6e0b5316e8..f0b675a95b 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java @@ -24,7 +24,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.lang.Nullable; +import org.springframework.util.AlternativeJdkIdGenerator; import org.springframework.util.Assert; +import org.springframework.util.IdGenerator; import org.springframework.web.socket.BinaryMessage; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.PingMessage; @@ -42,8 +44,11 @@ import org.springframework.web.socket.WebSocketSession; */ public abstract class AbstractWebSocketSession implements NativeWebSocketSession { + protected static final IdGenerator idGenerator = new AlternativeJdkIdGenerator(); + protected static final Log logger = LogFactory.getLog(NativeWebSocketSession.class); + private final Map attributes = new ConcurrentHashMap<>(); @Nullable diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java index 7142c12dd1..64dd0f2f80 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,6 @@ import org.springframework.http.HttpHeaders; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; -import org.springframework.util.ObjectUtils; import org.springframework.web.socket.BinaryMessage; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.PingMessage; @@ -55,8 +54,7 @@ import org.springframework.web.socket.adapter.AbstractWebSocketSession; */ public class JettyWebSocketSession extends AbstractWebSocketSession { - @Nullable - private String id; + private final String id; @Nullable private URI uri; @@ -91,13 +89,13 @@ public class JettyWebSocketSession extends AbstractWebSocketSession { */ public JettyWebSocketSession(Map attributes, @Nullable Principal user) { super(attributes); + this.id = idGenerator.generateId().toString(); this.user = user; } @Override public String getId() { - Assert.state(this.id != null, "WebSocket session is not yet initialized"); return this.id; } @@ -177,7 +175,6 @@ public class JettyWebSocketSession extends AbstractWebSocketSession { public void initializeNativeSession(Session session) { super.initializeNativeSession(session); - this.id = ObjectUtils.getIdentityHexString(getNativeSession()); this.uri = session.getUpgradeRequest().getRequestURI(); HttpHeaders headers = new HttpHeaders(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java index 77d1a6d405..0ab5517bfe 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,8 +50,7 @@ import org.springframework.web.socket.adapter.AbstractWebSocketSession; */ public class StandardWebSocketSession extends AbstractWebSocketSession { - @Nullable - private String id; + private final String id; @Nullable private URI uri; @@ -102,6 +101,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession @Nullable Principal user) { super(attributes); + this.id = idGenerator.generateId().toString(); headers = (headers != null ? headers : new HttpHeaders()); this.handshakeHeaders = HttpHeaders.readOnlyHttpHeaders(headers); this.user = user; @@ -112,7 +112,6 @@ public class StandardWebSocketSession extends AbstractWebSocketSession @Override public String getId() { - Assert.state(this.id != null, "WebSocket session is not yet initialized"); return this.id; } @@ -189,9 +188,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession public void initializeNativeSession(Session session) { super.initializeNativeSession(session); - this.id = session.getId(); this.uri = session.getRequestURI(); - this.acceptedProtocol = session.getNegotiatedSubprotocol(); List standardExtensions = getNativeSession().getNegotiatedExtensions(); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java index c3951516f9..db1a7a6823 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.web.socket.adapter.standard; +import java.net.URI; import javax.websocket.CloseReason; import javax.websocket.CloseReason.CloseCodes; import javax.websocket.MessageHandler; @@ -56,14 +57,15 @@ public class StandardWebSocketHandlerAdapterTests { @Test public void onOpen() throws Throwable { - given(this.session.getId()).willReturn("123"); + URI uri = URI.create("http://example.org"); + given(this.session.getRequestURI()).willReturn(uri); this.adapter.onOpen(this.session, null); verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession); verify(this.session, atLeast(2)).addMessageHandler(any(MessageHandler.Whole.class)); - given(this.session.getId()).willReturn("123"); - assertEquals("123", this.webSocketSession.getId()); + given(this.session.getRequestURI()).willReturn(uri); + assertEquals(uri, this.webSocketSession.getUri()); } @Test