javascript - Load image behind oauth2-authentication -
i develop javascript application uses oauth2 authentication. want load/show image server behind authentication mechanism. therefor send xmlhttp-request rest-server , uri of image response.
after request try append uri src of image , application responds 401.
how can tell browser reuse authentication image well?
this part of xmlhttp-request getting uri.
var xhr = new xmlhttprequest(); xhr.open("get", url, true); xhr.setrequestheader('content-type', 'application/json; charset=utf-8'); xhr.setrequestheader('authorization','bearer '+token); xhr.send(null); xhr.onreadystatechange = function() { console.log(xhr); if (xhr.readystate == 4 && xhr.status == 200) { var img = document.createelement('img'); img.src = xhr.responseurl; document.body.appendchild(img); } }
did forgot something?
an image static file , doubt you're going able oauth2 protection when request via simple url. while can add oauth2 information in url that's not thing because expose client data should private , secure.
maybe can consider serving image normal protected endpoint byte[] or base64 string can render in client if need protect image itself.
so have protected endpoint serves content of image itself. of course, you'd if need image private , secure. if don't let public , serve separate cdn. doesn't need behind oauth2 system.
Comments
Post a Comment