How do I trust a self signed certificate from an electron app?

你说的曾经没有我的故事 提交于 2019-11-27 03:50:25

问题


I have an electron app that syncs with a server I own at a https://XXX.XX.XX.XXX:port that has a self signed certificate. How can I trust that certificate from my electron app?

Right now I get:

Failed to load resource: net::ERR_INSECURE_RESPONSE

回答1:


You need to put the following code into your "shell" (core electron init) file:

    // 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);
    });

But this you allow insecure (invalid) certificates like self signed one.

Please note that this is NOT secure way of connecting to the server.

For more you can check documentation: https://electron.atom.io/docs/api/app/#event-certificate-error




回答2:


Subscribe to the certificate-error event emitted by the app module and verify your self signed cert in the event handler.




回答3:


Try this if 'certificate-error' event doesn't work:

if (process.env.NODE_ENV === 'DEV') {
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
}


来源:https://stackoverflow.com/questions/38986692/how-do-i-trust-a-self-signed-certificate-from-an-electron-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!