简体   繁体   English

如何使用javascript阅读烧瓶会话?

[英]How to read flask sessions using javascript?

I have a secured flask session 我有一个安全的烧瓶会议

session = "xC4tHoSZQVSHpVtnHUONYb/obAA=?USER_TOKEN=UycuZUp3Rndja1JnREFJQU1CZThwWVpqbkRWNHZpQW9QMlg0TzY5ZXN4MU5rTlZOaEM5RERuczBCRkRqSHFDY0YxTGZMSUM3WlNHdkxhZEpJUjZXcjh4ekZyUEQ5aUxFMEEuVGt0V3RqdTFKblVBVzV2SnRpSjd3M0NJZFdRJwpwMQou"

I am using angular.js cookies to retrieve the value but it gives me nothing. 我使用angular.js cookie来检索值,但它没有给我任何东西。

console.log('token - ' + $cookieStore.get('USER_TOKEN'));

How can I access the value of USER_TOKEN using Javascript? 如何使用Javascript访问USER_TOKEN的值?

I could be completely contradicted on this, but I've been of the understanding that you can't access the session data with Javascript because of some of the internals that the Werkzeug secure cookie module uses. 我可能完全反对这一点,但我一直认为你无法使用Javascript访问会话数据,因为Werkzeug安全cookie模块使用了一些内部功能。 I've got plans to try out this snippet as a workaround: 我有计划尝试这个片段作为解决方法:

http://flask.pocoo.org/snippets/51/ http://flask.pocoo.org/snippets/51/

But until I get a chance to try it I wouldn't know whether or not it could do some of the things lacking with the basic session module of Flask. 但是在我有机会尝试之前,我不知道它是否可以用Flask的基本会话模块做一些缺乏的事情。

Since the question was asked Flask switched to itsdangerous client side sessions by default. 由于问题被要求Flask默认切换到其危险的客户端会话。

As this is still the top google result for this question and i had some problems figuring it out myself, here is how to do it nowadays: 由于这仍然是这个问题的最佳谷歌结果,我有一些问题自己搞清楚,现在这是如何做到这一点:

function parse_session(){
    var cookie = Cookies('session');
    if(! cookie) return;
    // Is the content ziped ?
    var un_64 = "";
    if(cookie[0] == "."){
        var data = cookie.split('.')[1].replace(/_/g, '/').replace(/-/g, '+');
        un_b64 = atob(data);
        un_b64 = pako.inflate(un_b64, {to: 'string'});
    }else{
        var data = cookie.split('.')[0].replace(/_/g, '/').replace(/-/g, '+');
        un_b64 = atob(data);
    }
    return jQuery.parseJSON(un_b64);
}

This snippet uses jquery, cookie.js and paco (to unzip). 这个片段使用jquery,cookie.js和paco(解压缩)。 Flasks 'SESSION_COOKIE_HTTPONLY' config variable need to be set to False to be able to read the session on the client side. Flasks的'SESSION_COOKIE_HTTPONLY'配置变量需要设置为False才能读取客户端的会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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