Use random id for WebSocket sessions

Issue: SPR-17228
This commit is contained in:
Rossen Stoyanchev
2018-09-05 19:48:28 -04:00
parent ee559bb2c8
commit b17e7c321a
4 changed files with 17 additions and 16 deletions

View File

@@ -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;
@@ -41,8 +43,11 @@ import org.springframework.web.socket.WebSocketSession;
*/
public abstract class AbstractWebSocketSession<T> implements NativeWebSocketSession {
protected static final IdGenerator idGenerator = new AlternativeJdkIdGenerator();
protected static final Log logger = LogFactory.getLog(NativeWebSocketSession.class);
private final Map<String, Object> attributes = new ConcurrentHashMap<>();
@Nullable

View File

@@ -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<Session> {
@Nullable
private String id;
private final String id;
@Nullable
private URI uri;
@@ -91,13 +89,13 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
*/
public JettyWebSocketSession(Map<String, Object> 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<Session> {
public void initializeNativeSession(Session session) {
super.initializeNativeSession(session);
this.id = ObjectUtils.getIdentityHexString(getNativeSession());
this.uri = session.getUpgradeRequest().getRequestURI();
HttpHeaders headers = new HttpHeaders();

View File

@@ -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<Session> {
@Nullable
private String id;
private final String id;
@Nullable
private URI uri;
@@ -102,6 +101,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession<Session>
@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<Session>
@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<Session>
public void initializeNativeSession(Session session) {
super.initializeNativeSession(session);
this.id = session.getId();
this.uri = session.getRequestURI();
this.acceptedProtocol = session.getNegotiatedSubprotocol();
List<Extension> standardExtensions = getNativeSession().getNegotiatedExtensions();

View File

@@ -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