Html 如何使用基本身份验证设置 <img> 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3823357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the <img> tag with basic authentication
提问by Robert
I would like to display the image from a network camera on my web page, but the image is behind a HTTP basic authentication server.
我想在我的网页上显示来自网络摄像机的图像,但该图像位于 HTTP 基本身份验证服务器之后。
In Firefox and Chrome I can do this:
在 Firefox 和 Chrome 中,我可以这样做:
<img width="320" height="200" src="http://username:password@server/Path" />
But in Internet Explorer 8, I get an empty image box. If I use JQuery to set the src attribute, IE8 displays a blank src. It looks like IE8 is checking the string and rejecting it.
但是在 Internet Explorer 8 中,我得到一个空的图像框。如果我使用 JQuery 设置 src 属性,IE8 会显示一个空白的 src。看起来 IE8 正在检查字符串并拒绝它。
Is there a way to put the basic authentication credentials in the img tag?
有没有办法将基本身份验证凭据放在 img 标签中?
采纳答案by Spudley
Bottom line: Not all browsers allow this. It may work in some but not others.
底线:并非所有浏览器都允许这样做。它可能适用于某些但不适用于其他人。
But as someone else has said already, it's not very safe -- you're effectively giving the login and password details to anyone who browses the page. Not good.
但是正如其他人已经说过的那样,它不是很安全——您实际上是将登录名和密码详细信息提供给浏览该页面的任何人。不好。
A better option would be proxy it through the same server that you're providing the html code from, then the href in the <img>
tag could just be a local URL, and no-one need know where the image is actually coming from.
更好的选择是通过您提供 html 代码的同一服务器代理它,然后<img>
标签中的 href可能只是一个本地 URL,没有人需要知道图像的实际来源。
回答by Pablo Santa Cruz
You can load your img
with AJAX, using XMLHttpRequest. As you might know, XMLHttpRequest has a setRequestHeaders
method, so you will be able to manipulate headers for your request, hence, you will be able to do basic HTTP authentication.
您可以img
使用XMLHttpRequest加载AJAX 。您可能知道,XMLHttpRequest 有一个setRequestHeaders
方法,因此您将能够操作请求的标头,因此,您将能够进行基本的 HTTP 身份验证。
回答by Zach
Best way is to create a login page, then setup port forwarding on your router to display the camera. Does the camera come with web software?
最好的方法是创建一个登录页面,然后在路由器上设置端口转发以显示摄像头。相机是否带有网络软件?
回答by EricLaw
See http://support.microsoft.com/kb/834489and http://msdn.microsoft.com/en-us/library/ie/ee330735(v=vs.85).aspx#login_creds_urls
请参阅http://support.microsoft.com/kb/834489和http://msdn.microsoft.com/en-us/library/ie/ee330735(v=vs.85).aspx#login_creds_urls
Long story short, usernames/passwords are not formally supported under the HTTP/HTTPS protocol schemes and due to their use in phishing attacks, they were disabled in IE6 on XPSP2. Support can be manually re-enabled by the user using a registry override (FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
) but this is not recommended.
长话短说,HTTP/HTTPS 协议方案并未正式支持用户名/密码,并且由于它们用于网络钓鱼攻击,因此它们在 XPSP2 上的 IE6 中被禁用。用户可以使用注册表覆盖 ( FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
)手动重新启用支持,但不建议这样做。