Electron - 检测 ssl 证书错误并以编程方式忽略每个域/白名单的错误

Electron - detect ssl certificate errors and programmatically ignore errors per domain / whitelist

在 Chrome 中,如果您打开具有 expired/incorrect 证书的 https 页面,将显示警告,但用户仍然可以覆盖它并在会话期间将页面列入白名单.

在 Electron 应用程序中,使用不正确的证书访问 https 页面将导致空白页面且没有警告。

有没有办法:

  1. 检测页面何时因不正确的 SSL 而被拒绝加载(处理所有极端情况、所有类型的 SSL 错误等)
  2. 以编程方式将域列入白名单(在会话期间或根本不存在),以忽略安全警告并允许加载页面

这里有一个 certificate-error 回调函数,可用于添加逻辑以将某些域列入白名单

const { app } = require('electron')

app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
  if (url === 'https://github.com') {
    // Verification logic.
    event.preventDefault()
    callback(true)
  } else {
    callback(false)
  }
})

文档可在此处获得https://www.electronjs.org/docs/api/app#event-certificate-error