我正在尝试使用uri字符串“wss:// domain:port”连接到测试Web Socket ssl / wss服务器,使用Jetty(9.2。)和我创建的自签名证书(truststore.jks)密钥工具:
keytool -genkeypair -alias certificatekey -keyalg RSA -validity 365 -keystore keystore.jks
keytool -export -alias certificatekey -keystore keystore.jks -rfc -file selfsignedcert.cer
keytool -import -alias certificatekey -file selfsignedcert.cer -keystore truststore.jks
并使用客户端代码
public static void main(String[] args) {
SslContextFactory ssl = new SslContextFactory();
ssl.setKeyStoreResource(Resource.newResource("truststore.jks"));
ssl.setKeyStorePassword(password);
ssl.setKeyManagerPassword(password);
WebSocketClient client = new WebSocketClient(ssl);
MySocket socket = new MySocket();
client.start();
URI echoUri = new URI("wss://domain:port");
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket, echoUri);
socket.awaitClose(10, TimeUnit.SECONDS);
client.stop();
}
使用我的套接字客户端代码
@WebSocket
public class MySocket {
...
@OnWebSocketConnect
public void onConnect(Session session) {
System.out.println("Got connect: "+session);
}
}
不幸的是,没有连接消息(“Got connect ...”)来自使用@OnWebSocket注释的套接字方法onConnect。当使用不安全的服务器时(ws而不是wss),它可以工作。