Skip to content

Commit d92c74d

Browse files
sokomishalovrstoyanchev
authored andcommitted
Add cookies to the WebSocket HandshakeInfo
See gh-26674
1 parent 4f14291 commit d92c74d

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/socket/HandshakeInfo.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.util.Collections;
2323
import java.util.Map;
2424

25+
import org.springframework.http.HttpCookie;
26+
import org.springframework.util.CollectionUtils;
27+
import org.springframework.util.MultiValueMap;
2528
import reactor.core.publisher.Mono;
2629

2730
import org.springframework.http.HttpHeaders;
@@ -44,6 +47,8 @@ public class HandshakeInfo {
4447

4548
private final HttpHeaders headers;
4649

50+
private final MultiValueMap<String, HttpCookie> cookies;
51+
4752
@Nullable
4853
private final String protocol;
4954

@@ -67,6 +72,7 @@ public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal, @N
6772
this(uri, headers, principal, protocol, null, Collections.emptyMap(), null);
6873
}
6974

75+
7076
/**
7177
* Constructor targetting server-side use with extra information about the
7278
* handshake, the remote address, and a pre-existing log prefix for
@@ -81,17 +87,39 @@ public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal, @N
8187
* messages, if any.
8288
* @since 5.1
8389
*/
90+
@Deprecated
8491
public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal,
85-
@Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
86-
Map<String, Object> attributes, @Nullable String logPrefix) {
92+
@Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
93+
Map<String, Object> attributes, @Nullable String logPrefix) {
94+
this(uri, headers, CollectionUtils.toMultiValueMap(Collections.emptyMap()), principal, protocol, remoteAddress, attributes, logPrefix);
95+
}
8796

97+
/**
98+
* Constructor targetting server-side use with extra information about the
99+
* handshake, the remote address, and a pre-existing log prefix for
100+
* correlation.
101+
* @param uri the endpoint URL
102+
* @param headers request headers for server or response headers or client
103+
* @param cookies request cookies for server
104+
* @param principal the principal for the session
105+
* @param protocol the negotiated sub-protocol (may be {@code null})
106+
* @param remoteAddress the remote address where the handshake came from
107+
* @param attributes initial attributes to use for the WebSocket session
108+
* @param logPrefix log prefix used during the handshake for correlating log
109+
* messages, if any.
110+
* @since 5.4
111+
*/
112+
public HandshakeInfo(URI uri, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
113+
Mono<Principal> principal, @Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
114+
Map<String, Object> attributes, @Nullable String logPrefix) {
88115
Assert.notNull(uri, "URI is required");
89116
Assert.notNull(headers, "HttpHeaders are required");
90117
Assert.notNull(principal, "Principal is required");
91118
Assert.notNull(attributes, "'attributes' is required");
92119

93120
this.uri = uri;
94121
this.headers = headers;
122+
this.cookies = cookies;
95123
this.principalMono = principal;
96124
this.protocol = protocol;
97125
this.remoteAddress = remoteAddress;
@@ -115,6 +143,13 @@ public HttpHeaders getHeaders() {
115143
return this.headers;
116144
}
117145

146+
/**
147+
* Return the handshake HTTP cookies.
148+
*/
149+
public MultiValueMap<String, HttpCookie> getCookies() {
150+
return this.cookies;
151+
}
152+
118153
/**
119154
* Return the principal associated with the handshake HTTP request.
120155
*/

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
import org.springframework.context.Lifecycle;
3333
import org.springframework.http.HttpHeaders;
3434
import org.springframework.http.HttpMethod;
35+
import org.springframework.http.HttpCookie;
3536
import org.springframework.http.server.reactive.ServerHttpRequest;
3637
import org.springframework.lang.Nullable;
3738
import org.springframework.util.Assert;
3839
import org.springframework.util.ClassUtils;
3940
import org.springframework.util.ReflectionUtils;
4041
import org.springframework.util.StringUtils;
42+
import org.springframework.util.MultiValueMap;
43+
import org.springframework.util.LinkedMultiValueMap;
4144
import org.springframework.web.reactive.socket.HandshakeInfo;
4245
import org.springframework.web.reactive.socket.WebSocketHandler;
4346
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
@@ -282,10 +285,12 @@ private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttp
282285
// the server implementation once the handshake HTTP exchange is done.
283286
HttpHeaders headers = new HttpHeaders();
284287
headers.addAll(request.getHeaders());
288+
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
289+
cookies.addAll(request.getCookies());
285290
Mono<Principal> principal = exchange.getPrincipal();
286291
String logPrefix = exchange.getLogPrefix();
287292
InetSocketAddress remoteAddress = request.getRemoteAddress();
288-
return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix);
293+
return new HandshakeInfo(uri, headers, cookies, principal, protocol, remoteAddress, attributes, logPrefix);
289294
}
290295

291296
}

0 commit comments

Comments
 (0)