Skip to content

Commit 9da3d25

Browse files
committed
Fix websocket-tls-basic-auth example test to pass SSLContext via ClientEndpointConfig
Tomcat 11's WsWebSocketContainer reads jakarta.websocket.ClientEndpointConfig#getSSLContext() directly and no longer honours the legacy org.apache.tomcat.websocket.SSL_TRUSTSTORE / SSL_TRUSTSTORE_PWD user-properties, so the test was falling back to the JDK default truststore and failing the handshake against the example's self-signed cert. Build an SSLContext from the bundled keystore.jks and set it via ClientEndpointConfig.Builder#sslContext(..).
1 parent b021f32 commit 9da3d25

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

examples/websocket-tls-basic-auth/src/test/java/org/superbiz/websockets/WebSocketResourceTest.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@
3333
import jakarta.websocket.EndpointConfig;
3434
import jakarta.websocket.MessageHandler.Whole;
3535
import jakarta.websocket.Session;
36+
import javax.net.ssl.SSLContext;
37+
import javax.net.ssl.TrustManagerFactory;
3638
import java.io.File;
39+
import java.io.FileInputStream;
40+
import java.io.InputStream;
3741
import java.net.URI;
3842
import java.net.URL;
43+
import java.security.KeyStore;
3944
import java.util.List;
4045
import java.util.Map;
4146
import java.util.concurrent.CountDownLatch;
@@ -106,16 +111,25 @@ public void beforeRequest(Map<String, List<String>> headers) {
106111
}
107112
};
108113

114+
// Trust the server's self-signed certificate by building an SSLContext from the bundled
115+
// keystore. Tomcat 11's WsWebSocketContainer reads the SSLContext directly from
116+
// ClientEndpointConfig#getSSLContext() and no longer honours the legacy
117+
// org.apache.tomcat.websocket.SSL_TRUSTSTORE / SSL_TRUSTSTORE_PWD user-properties.
118+
final KeyStore trustStore = KeyStore.getInstance("PKCS12");
119+
try (InputStream in = new FileInputStream("src/main/conf/keystore.jks")) {
120+
trustStore.load(in, "123456".toCharArray());
121+
}
122+
final TrustManagerFactory tmf = TrustManagerFactory.getInstance(
123+
TrustManagerFactory.getDefaultAlgorithm());
124+
tmf.init(trustStore);
125+
final SSLContext sslContext = SSLContext.getInstance("TLS");
126+
sslContext.init(null, tmf.getTrustManagers(), null);
127+
109128
ClientEndpointConfig authorizationConfiguration = ClientEndpointConfig.Builder.create()
110129
.configurator(configurator)
130+
.sslContext(sslContext)
111131
.build();
112132

113-
//use same keystore as the server
114-
authorizationConfiguration.getUserProperties().put("org.apache.tomcat.websocket.SSL_TRUSTSTORE",
115-
"src/main/conf/keystore.jks");
116-
authorizationConfiguration.getUserProperties().put("org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD",
117-
"123456");
118-
119133
Session session = ContainerProvider.getWebSocketContainer()
120134
.connectToServer(
121135
endpoint,

0 commit comments

Comments
 (0)