简体   繁体   中英

How can I make hls.js send the cookie in request header upon requesting encryption key

I tried

var config = {
       xhrSetup: function (xhr) {
          xhr.withCredentials = true; // do send cookies
       }
    };

but this doesn't work. The Cookie is sent on playlist request and on segment request no matter if I enable credentials in the xhrSetup or not... but not on key request. For securing the content I need to identify the user before handing out the key. It's possible to set an "X-Cookie" as i wrote in the answer, but how would I send the session cookie?

I set an "X-Cookie" like this:

    var config = {
        debug: true,
        xhrSetup: function (xhr,url) {
            xhr.withCredentials = true; // do send cookie
            xhr.setRequestHeader("X-Cookie", read_cookie('SESSIONID'));
        }
    };

on the other side it needs

Header set Access-Control-Allow-Headers "X-Cookie"

the read_cookie function:

function read_cookie(key) {
   var result;
   (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? (result[1]) : null;
   console.log('result : ' + result[1]);
   return result[1]
}

is there a way to send a Cookie value though?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM