Skip to content

Commit 7481d73

Browse files
committed
WebFlux HandshakeInfo exposes the remoteAddress
Issue: SPR-17192
1 parent 288a9ec commit 7481d73

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.reactive.socket;
1818

19+
import java.net.InetSocketAddress;
1920
import java.net.URI;
2021
import java.security.Principal;
2122
import java.util.Collections;
@@ -46,36 +47,43 @@ public class HandshakeInfo {
4647
@Nullable
4748
private final String protocol;
4849

50+
@Nullable
51+
private final InetSocketAddress remoteAddress;
52+
4953
private final Map<String, Object> attributes;
5054

5155
@Nullable
5256
private final String logPrefix;
5357

5458

5559
/**
56-
* Constructor with information about the handshake.
60+
* Constructor with basic information about the handshake.
5761
* @param uri the endpoint URL
5862
* @param headers request headers for server or response headers or client
5963
* @param principal the principal for the session
6064
* @param protocol the negotiated sub-protocol (may be {@code null})
6165
*/
6266
public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal, @Nullable String protocol) {
63-
this(uri, headers, principal, protocol, Collections.emptyMap(), null);
67+
this(uri, headers, principal, protocol, null, Collections.emptyMap(), null);
6468
}
6569

6670
/**
67-
* Constructor with information about the handshake.
71+
* Constructor targetting server-side use with extra information about the
72+
* handshake, the remote address, and a pre-existing log prefix for
73+
* correlation.
6874
* @param uri the endpoint URL
6975
* @param headers request headers for server or response headers or client
7076
* @param principal the principal for the session
7177
* @param protocol the negotiated sub-protocol (may be {@code null})
78+
* @param remoteAddress the remote address where the handshake came from
7279
* @param attributes initial attributes to use for the WebSocket session
7380
* @param logPrefix log prefix used during the handshake for correlating log
7481
* messages, if any.
7582
* @since 5.1
7683
*/
7784
public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal,
78-
@Nullable String protocol, Map<String, Object> attributes, @Nullable String logPrefix) {
85+
@Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
86+
Map<String, Object> attributes, @Nullable String logPrefix) {
7987

8088
Assert.notNull(uri, "URI is required");
8189
Assert.notNull(headers, "HttpHeaders are required");
@@ -86,6 +94,7 @@ public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal,
8694
this.headers = headers;
8795
this.principalMono = principal;
8896
this.protocol = protocol;
97+
this.remoteAddress = remoteAddress;
8998
this.attributes = attributes;
9099
this.logPrefix = logPrefix;
91100
}
@@ -123,6 +132,16 @@ public String getSubProtocol() {
123132
return this.protocol;
124133
}
125134

135+
/**
136+
* For a server-side session this is the remote address where the handshake
137+
* request came from.
138+
* @since 5.1
139+
*/
140+
@Nullable
141+
public InetSocketAddress getRemoteAddress() {
142+
return this.remoteAddress;
143+
}
144+
126145
/**
127146
* Attributes extracted from the handshake request to be added to the
128147
* WebSocket session.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.reactive.socket.server.support;
1818

19+
import java.net.InetSocketAddress;
1920
import java.net.URI;
2021
import java.security.Principal;
2122
import java.util.Collections;
@@ -274,7 +275,8 @@ private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttp
274275
HttpHeaders headers = request.getHeaders();
275276
Mono<Principal> principal = exchange.getPrincipal();
276277
String logPrefix = exchange.getLogPrefix();
277-
return new HandshakeInfo(uri, headers, principal, protocol, attributes, logPrefix);
278+
InetSocketAddress remoteAddress = request.getRemoteAddress();
279+
return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix);
278280
}
279281

280282
}

0 commit comments

Comments
 (0)