Skip to content

Commit d616c66

Browse files
committed
Remove session on 4xx response from WebSocket handshake
Closes gh-25608
1 parent 94c91c9 commit d616c66

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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,6 +34,7 @@
3434
import org.springframework.http.HttpStatus;
3535
import org.springframework.http.server.ServerHttpRequest;
3636
import org.springframework.http.server.ServerHttpResponse;
37+
import org.springframework.http.server.ServletServerHttpResponse;
3738
import org.springframework.lang.Nullable;
3839
import org.springframework.scheduling.TaskScheduler;
3940
import org.springframework.util.Assert;
@@ -270,6 +271,7 @@ else if (transportType.supportsCors()) {
270271
}
271272

272273
SockJsSession session = this.sessions.get(sessionId);
274+
boolean isNewSession = false;
273275
if (session == null) {
274276
if (transportHandler instanceof SockJsSessionFactory) {
275277
Map<String, Object> attributes = new HashMap<>();
@@ -278,6 +280,7 @@ else if (transportType.supportsCors()) {
278280
}
279281
SockJsSessionFactory sessionFactory = (SockJsSessionFactory) transportHandler;
280282
session = createSockJsSession(sessionId, sessionFactory, handler, attributes);
283+
isNewSession = true;
281284
}
282285
else {
283286
response.setStatusCode(HttpStatus.NOT_FOUND);
@@ -311,6 +314,14 @@ else if (transportType.supportsCors()) {
311314
}
312315

313316
transportHandler.handleRequest(request, response, handler, session);
317+
318+
if (isNewSession && (response instanceof ServletServerHttpResponse)) {
319+
int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus();
320+
if (HttpStatus.valueOf(status).is4xxClientError()) {
321+
this.sessions.remove(sessionId);
322+
}
323+
}
324+
314325
chain.applyAfterHandshake(request, response, null);
315326
}
316327
catch (SockJsException ex) {

0 commit comments

Comments
 (0)