我正在尝试使用 websocket 连接到使用 react-native 的 TLS 服务器。下面是我的代码(在 windows + android 上运行):
var ws = new WebSocket('wss://hub.fingi-staging.com:20020',{
rejectUnauthorized: false
});
ws.onopen = () => {
// connection opened
ws.send('something'); // send a message
};
ws.onmessage = (e) => {
// a message was received
console.log('message : ' + e.data);
};
ws.onerror = (e) => {
// an error occurred
console.log('error:'+e.message);
};
ws.onclose = (e) => {
// connection closed
console.log('close:'+e.code, e.reason);
};
但是,它失败了:error:java.security.cert.CertPathValidatorException: Trust anchor for certification path not found
。这是因为服务器使用自签名证书。
有什么办法可以解决这个问题吗?
最佳答案
回复有点晚,但希望这可以为其他人指明正确的方向。
我相信您遇到的错误表明您缺少证书链文件,该文件用于验证用于签署服务器证书的 CA 是否有效,即信任链是否有效。
但是,如果您使用的是自签名证书,这通常会失败(至少很麻烦)。你可以看看here如果您需要一些帮助来生成一些自签名证书和适当的证书链。此外,查看您是否需要通过让客户端在连接时使用该文件作为参数来指定受信任的 CA。
我一直在努力使用自签名证书设置安全的 websocket 服务器(出于开发目的,在生产中必须使用适当的证书/CA)但没有取得太大成功并恢复使用非 TLS网络套接字服务器。
关于ssl - React-native websocket TLS 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40426843/