我正在将网站迁移到 https,但我们的广告转换网络经常通过 http 发送不安全的广告,从而导致出现“混合内容”警告。用 service worker 阻止/ stub 所有 http 请求是个好主意吗?
最佳答案
您可以使用 CSP 来阻止所有非 https 请求:
内容安全策略:阻止所有混合内容
它将http请求重写为https。如果您只想阻止它们,类似的方法应该有效:
Content-Security-Policy:default-src 'self' data:'unsafe-inline' 'unsafe-eval' https:;
更多详情:https://scotthelme.co.uk/migrating-from-http-to-https-ease-the-pain-with-csp-and-hsts/
https://report-uri.io/home/generate (我认为这比使用 Service Worker 更好...)
警告:如果滥用,CSP 可以阻止合法请求和/或内联脚本/css/插件...
关于javascript - 使用 Service Worker 阻止不安全的 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38585890/