ssl - React-native websocket TLS 连接

标签 ssl websocket react-native

我正在尝试使用 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/

相关文章:

ssl - 如何在TCP模式下用haproxy传递自定义SNI

php - Ratchet 服务器与长轮询

javascript - 使用 WebSockets 获取客户端列表

react-native - Picker onValueChange 不是在每次选择时都被调用

javascript - 对于 React Native 中的 navigator.geolocation, native 模块不能为 null

c - 服务器 key 交换消息 EAP-TLS 中的服务器公钥

ruby - 为瘦服务器和 sinatra 启用 SSL

authentication - OpenSSL:是否可以附加额外的字符串进行身份验证?

apache - 将 Tomcat http 连接器限制为 Apache 服务器

android - 使用 RxJS 响应 native