开发者

How to set the <img> tag with basic authentication

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.

In Firefox and Chrome I can do this:

<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 t开发者_如何学运维o set the src attribute, IE8 displays a blank src. It looks like IE8 is checking the string and rejecting it.

Is there a way to put the basic authentication credentials in the img tag?


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.


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.


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?


See http://support.microsoft.com/kb/834489 and 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.


Try http proxy.

On server side, enable tinyProxy, create ReversePath to basic authentication server in configuration like:

AddHeader "Authorization" "Basic dXNlcjpwYXNz"
ReversePath "/foo/" "http://somewhere:3480/foo/"

dXNlcjpwYXNz is base64 encoded string from user:pass

Enable reverse proxy in Apache or NGINX to tinyProxy path http://localhot:8888/foo/

Img Source accessable from local server instead of old way deprecated, without http auth pop-up or CORS error.

http://user:pass@somewhere:3480/foo/DEST.jpg


ajax add http header works!

pictureUrl = "https://somewhere/file.jpg";
var oReq = new XMLHttpRequest();
oReq.open("GET", pictureUrl, true);
oReq.setRequestHeader("Authorization", "Basic " + btoa("UserName"+":"+"Password"));
// use multiple setRequestHeader calls to set multiple values
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
  var arrayBuffer = oReq.response; // Note: not oReq.responseText
  if (arrayBuffer) {
    var u8 = new Uint8Array(arrayBuffer);
    var b64encoded = btoa(String.fromCharCode.apply(null, u8));
    var mimetype="image/jpeg"; // or whatever your image mime type is
    document.getElementById("iOdata").src="data:"+mimetype+";base64,"+b64encoded;
  }
};
oReq.send(null);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜