diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java index aa3026857c..19c401d6b2 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java @@ -73,11 +73,9 @@ public interface WebSocketSession extends Closeable { /** * Return the address on which the request was received. *

Note: The localAddress is not always possible to access, - * which is the case with the Standard WebSocket client. In 6.2.x + * which is the case with the Standard WebSocket client API, and accordingly * {@link org.springframework.web.socket.client.standard.StandardWebSocketClient} - * returns an address based on the local host and the port of the target - * address (not the same as the local port). In 7.0, the same will return - * {@code null} instead. + * returns {@code null}. */ @Nullable InetSocketAddress getLocalAddress(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java index aa9557c977..ae58599a2c 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -149,12 +149,10 @@ public class StandardWebSocketClient extends AbstractWebSocketClient { HttpHeaders headers, final URI uri, List protocols, List extensions, Map attributes) { - int port = getPort(uri); - InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port); - InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port); + InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), getPort(uri)); - StandardWebSocketSession session = new StandardWebSocketSession(headers, - attributes, localAddress, remoteAddress); + StandardWebSocketSession session = + new StandardWebSocketSession(headers, attributes, null, remoteAddress); ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create() .configurator(new StandardWebSocketClientConfigurator(headers)) diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/client/standard/StandardWebSocketClientTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/client/standard/StandardWebSocketClientTests.java index bc9314775c..53354c1f9d 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/client/standard/StandardWebSocketClientTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/client/standard/StandardWebSocketClientTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -60,18 +60,11 @@ class StandardWebSocketClientTests { void getLocalAddress() throws Exception { URI uri = URI.create("ws://localhost/abc"); WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get(); + assertThat(session.getLocalAddress()).isNull(); - assertThat(session.getLocalAddress()).isNotNull(); - assertThat(session.getLocalAddress().getPort()).isEqualTo(80); - } - - @Test - void getLocalAddressWss() throws Exception { - URI uri = URI.create("wss://localhost/abc"); - WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get(); - - assertThat(session.getLocalAddress()).isNotNull(); - assertThat(session.getLocalAddress().getPort()).isEqualTo(443); + uri = URI.create("wss://localhost/abc"); + session = this.wsClient.execute(this.wsHandler, this.headers, uri).get(); + assertThat(session.getLocalAddress()).isNull(); } @Test @@ -88,7 +81,7 @@ class StandardWebSocketClientTests { assertThat(session.getRemoteAddress()).isNotNull(); assertThat(session.getRemoteAddress().getHostName()).isEqualTo("localhost"); - assertThat(session.getLocalAddress().getPort()).isEqualTo(443); + assertThat(session.getLocalAddress()).isNull(); } @Test