我已经尝试将现有的websocket实现从使用ws更改为wss大约一周了,但运气不好。我在网上尝试了很多例子,并得到了各种各样的错误。这里的最终目标是让一个网页通过ssl连接到运行websocket的java应用程序。所以首先我想,在我使用网页替换java客户机之前,最好尝试让两个java应用程序能够连接。
我已使用以下方法生成密钥库、证书和信任存储:
keytool -genkey -keyalg RSA -validity 3650 -keystore "C:\users\user\Desktop\fb1dasslkeystore.jks" -storepass "test11" -keypass "test11" -alias "FB1DASSL" -dname "CN=127.0.0.1, OU=CA, O=Company, L=Location, S=State, C=USA"
在这里,我按照建议将其迁移到pkcs12:
keytool -importkeystore -srckeystore C:\users\user\Desktop\fb1dasslkeystore.jks -destkeystore C:\users\user\Desktop\fb1dasslkeystore.jks -deststoretype pkcs12
keytool -export -alias FB1DASSL -keystore C:\users\user\Desktop\fb1dasslkeystore.jks -file C:\users\user\Desktop\fb1da.cert
keytool -import -file C:\users\user\Desktop\fb1da.cert -alias FB1DASSL -keystore C:\users\user\desktop\mytruststore.jts
然后,我使用“管理计算机证书”将证书导入windows中的受信任证书点,并将其加载到受信任的根证书颁发机构下(用于稍后的web内容)。
然后尝试运行我的服务器和客户端,但不断收到“连接被拒绝”。或者我获取javax.net.ssl.sslhandshakeexception:pkix路径生成失败:sun.security.provider.certpath.suncertpathbuilderexception:无法找到请求目标的有效证书路径。
testservermain
public class TestServer
{
public static void main(String[] args)
{
String STORETYPE = "JKS";
String KEYSTORE = new File("C:\\users\\user\\Desktop\\fb1dasslkeystore.jks").getAbsolutePath();
String STOREPASSWORD = "test11";
String KEYPASSWORD = "test11";
// now start the websocket server
KeyStore ks;
try
{
jWsServer ws = new jWsServer(new InetSocketAddress("127.0.0.1", 8888));
ks = KeyStore.getInstance(STORETYPE);
File kf = new File(KEYSTORE);
ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, KEYPASSWORD.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext sslContext = null;
sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
ws.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
ws.start();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(String.format("Websocket server started on port = %d", 8888));
}
}
服务器类
public class jWsServer extends WebSocketServer
{
static Logger errorLog = LogManager.getLogger(jWsServer.class.getName());
/**
* Constructor, where caller specifies non-default port number
*
* @param port socket port number
* @throws UnknownHostException
*/
public jWsServer(int port) throws UnknownHostException
{
super(new InetSocketAddress(port));
}
/**
* Constructor, where caller specifies non-default socket address
*
* @param address socket address
*/
public jWsServer(InetSocketAddress address)
{
super(address);
}
/**
* Event that fires when a client sends a message
*
* @param conn client WebSocket object handle
* @param message message string, should be JSON string for a request
*/
@Override
public void onMessage(WebSocket conn, String message)
{
try
{
System.out.println("Message Recieved : " + message);
}
catch (Exception ex)
{
System.out.println("ERROR: Can't add request to session " + conn);
}
}
/**
* Event that fires when a connection error occurs
*
* @param conn client WebSocket object handle
* @param ex exception that occurred
*/
@Override
public void onError(WebSocket conn, Exception ex)
{
if (conn != null)
{
System.out.println("ERROR: " + conn + " onError()" + ex.getMessage());
}
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote)
{
System.out.println("Disconnecting client connId=" + conn.hashCode() + " Reason=" + code);
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake)
{
try
{
System.out.println("Connecting client connId=" + conn.hashCode());
}
catch (Exception ex)
{
System.out.println("ERROR: Can't create session or add it to queue");
}
errorLog.info("Websocket open.");
}
@Override
public void onStart()
{
// TODO Auto-generated method stub
System.out.println(this.getAddress().getHostString());
}
}
主客户端
public class Test
{
public static void main(String[] args)
{
String location;
location = "wss://127.0.0.1:8888";
System.out.println("Using location: \'" + location + "\'");
try
{
new MyClient(location);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端类
public class MyClient
{
private WebSocketClient cc;
public MyClient(String location)
{
try
{
cc = new WebSocketClient(new URI(location))
{
@Override
public void onMessage(String message)
{
System.out.println("got: " + message + "\n");
}
@Override
public void onOpen(ServerHandshake handshake)
{
System.out.println("You are connected to ChatServer: " + getURI() + "\n");
}
@Override
public void onClose(int code, String reason, boolean remote)
{
System.out.println(
"You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason
+ "\n");
}
@Override
public void onError(Exception ex)
{
System.out.println("Exception occurred ...\n" + ex + "\n");
ex.printStackTrace();
}
};
cc.connect();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!