我如何信任来自电子应用程序的自签名证书?

jtl*_*sey 16 ssl https self-signed electron

我有一个电子应用程序,它与我拥有的服务器同步,https://XXX.XX.XX.XXX:port具有自签名证书.我怎么能相信我的电子应用程序的证书?

现在我得到:

Failed to load resource: net::ERR_INSECURE_RESPONSE
Run Code Online (Sandbox Code Playgroud)

Pet*_*nar 26

您需要将以下代码放入"shell"(核心电子初始化)文件中:

    // SSL/TSL: this is the self signed certificate support
    app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
        // On certificate error we disable default behaviour (stop loading the page)
        // and we then say "it is all fine - true" to the callback
        event.preventDefault();
        callback(true);
    });
Run Code Online (Sandbox Code Playgroud)

但是这允许你自己签名的不安全(无效)证书.

请注意,这不是连接到服务器的安全方式.

有关更多信息,请查看文档:https://electron.atom.io/docs/api/app/#event-certificate-error

  • 这仅在请求来自`renderer`进程时才有效,不是吗?有没有办法在“ main”进程中拦截来自“ request”或“ axios”的请求? (3认同)

Vad*_*gon 8

订阅模块certificate-error发出的事件,app并在事件处理程序中验证您的自签名证书.


小智 6

如果'certificate-error'事件不起作用,请尝试以下操作:

if (process.env.NODE_ENV === 'DEV') {
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
}
Run Code Online (Sandbox Code Playgroud)