javascript - 在 JavaScript 中检查 SSL 证书是否有效

标签 javascript ssl certificate

如果主机的 SSL 证书有效,有没有办法检查 JavaScript? (非阻塞)

在我的例子中,我想显示:“你也可以使用 https://..”如果通过 JavaScript 我可以向 https://my_url 发出请求而不被要求接受不受信任的证书。

这可以异步完成吗?

最佳答案

看这里:https://support.mozilla.org/pl/questions/923494

<img src="https://the_site/the_image" onerror="redirectToCertPage()">

此解决方案已经过测试并适用于当前版本的 FF 和 Chrome(截至 2022 年):

<script> var sslCertTrusted = false; </script>
<script src="https://example.com/ssltest.js"></script>
<script> 
    if (!sslCertTrusted) 
    {
        alert('Sorry, you need to install the certificate first.');
        window.location.replace('http://example.com/cert_install_instructions/');
    }
    else
    {
        // alert('Redirecting to secure connection')
        window.location.replace('https://example.com/');
    }
<script>

您当然需要让您的网络服务器在 URL https://example.com/ssltest.js 下返回此代码:

sslCertTrusted = true;

我不太确定细节。但是我已经看到用于检测广告拦截等的类似技术。如果变量不能被另一个脚本修改,您可能需要搭载 window 对象,但通常会进行上述证明概念工作留给读者作为练习。

关于javascript - 在 JavaScript 中检查 SSL 证书是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2618944/

相关文章:

javascript - 添加第二张幻灯片后幻灯片停止工作

javascript - AngularJS: Controller 之间的数据传递和范围问题

javascript - 在选项卡式导航中打开模式 - Bootstrap 3 & Rails 4

email - 查看端口 587、25、110、465、995、143、993 上的 SSL 证书

javascript - 谷歌图表; Uncaught Error : Not an array

ssl - MariaDB 上的 have_ssl 保持禁用状态,无法在 Debian 和 YaSSL 上启用 SSL

android - SSL 固定客户端加密

c - 设计 EAP-TLS 客户端问候消息

c# - 为什么我在使用 IIS 时获得 Cryptography_CSP_NoPrivateKey 而在使用 VS 开发服务器时却没有?

ssl - 本地主机上的 Websocket 通过 https ://problems