如果主机的 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/