【问题标题】:How can I make hls.js send the cookie in request header upon requesting encryption key如何在请求加密密钥时让 hls.js 在请求标头中发送 cookie
【发布时间】:2016-04-27 13:43:03
【问题描述】:

我试过了

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

但这不起作用。 无论我是否在 xhrSetup 中启用凭据,Cookie 都会在播放列表请求和分段请求中发送……但不是在密钥请求中。 为了保护内容,我需要在分发密钥之前识别用户。 正如我在答案中所写,可以设置“X-Cookie”,但我将如何发送会话 cookie?

【问题讨论】:

    标签: javascript cookies http-live-streaming m3u8


    【解决方案1】:

    我这样设置“X-Cookie”:

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

    另一方面它需要

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

    read_cookie 函数:

    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]
    }
    

    有没有办法发送 Cookie 值?

    【讨论】:

    • 在“read_cookie(key)”函数中只返回这个:return key + '=' + result[1]
    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2016-06-06
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多