使用 CDN 时如何通过 https 确保页面安全?
我的网页在内部使用相对 url,但我也使用 googlemaps 和 jquery 的直接链接。
那么我该如何确保页面的 https 安全呢?
谢谢,
马特
My web page uses relative urls internally but also I use direct links to googlemaps and jquery.
how do I then make the page secure for https?
Thanks,
Mat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查当前使用的是 HTTPS 还是 HTTP,并根据情况输出正确的 URL,但最好的办法是完全省略协议和冒号,这将使浏览器使用与该页面相同的内容正在从 提供服务。例如,假设您有以下 URL:
http://mycdn.com/project/version.script。 <
在您的代码中,您只需使用:
//mycdn.com/project/version.script.js
即
: 脚本 src="//mycdn.com/project/version.script.js">< /脚本>
您的 HTTPS 页面将抓取 https://mycdn.com/project/version.script.js 而您的 http 页面将使用 http://mycdn.com/project/version.script.js< /a>
You could check whether you are currently using HTTPS or HTTP and outputting the correct URL depending on that, but the best thing to do is to omit the protocol altogether, and the colon, which will leave the browser to use the same as what that page is being served from. For instance let's say you has this URL:
http://mycdn.com/project/version.script.js
In your code you would just use:
//mycdn.com/project/version.script.js
i.e.:
< script src="//mycdn.com/project/version.script.js">< /script>
Your HTTPS pages would grab https://mycdn.com/project/version.script.js whereas your http pages would use http://mycdn.com/project/version.script.js
您只需指向正在加载的外部资源的 HTTPS 版本即可确保整个页面的安全。
jQuery 的 Microsoft CDN 支持 SSL: https://ajax.aspnetcdn.com /ajax/jQuery/jquery-1.7.1.min.js
(来源:http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery)
Google 地图也可以通过 SSL 连接免费使用:
(来源:http: //googlegeodevelopers.blogspot.com.au/2011/03/maps-apis-over-ssl-now-available-to-all.html)
You'll just need to point to the HTTPS versions of the external assets you're loading to make the entire page secure.
jQuery's Microsoft CDN supports SSL: https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js
(source: http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery)
Google Maps can also be used over an SSL connection for free:
(source: http://googlegeodevelopers.blogspot.com.au/2011/03/maps-apis-over-ssl-now-available-to-all.html)