【问题标题】:koajs session - where is the session stored?koajs session - 会话存储在哪里?
【发布时间】:2015-06-23 20:32:17
【问题描述】:

我正在使用this 模块进行 koajs 会话。

我检查了源代码,但我真的无法理解。 我想知道它在哪里保存会话数据,因为我没有看到任何文件创建,并且当服务器重新启动时,会话数据仍然存在。

我感觉它是将数据保存在 cookie 本身中,然后我看到它创建了两个带有乱码的 cookie。

现在,它是在 cookie 本身中编码数据(不安全)还是以我还不理解的方式将数据保存在服务器上?

【问题讨论】:

    标签: session koa


    【解决方案1】:

    根据 koa-session 库中的this section of code,将会话数据编码为 JSON,然后转为 base64,然后附加到 cookie。

    Session.prototype.save = function(){
      var ctx = this._ctx;
      var json = this.toJSON();
      var opts = ctx.sessionOptions;
      var key = ctx.sessionKey;
      // set expire into cookie value
      var maxAge = opts.maxAge || ONE_DAY;
      json._expire = maxAge + Date.now();
      json._maxAge = maxAge;
      json = encode(json);
      debug('save %s', json);
      ctx.cookies.set(key, json, opts); // <-- this is where the session is being saved
    };
    

    【讨论】:

    • 知道为什么 Koa 会话在不签署 cookie 的情况下使用秘密吗?
    【解决方案2】:

    我通过向 Koa 服务器发送 this.session.passport.id 和

    来做到这一点
    yield this.render('template',{id: this.session.passport.id});
    

    并在存储 id 的客户端创建了一个 cookie。 当服务器请求客户端时,我通过 POST 或 GET 将这个 id 与请求一起发送,该请求由路由解析:

    public.get('/resource/:id',function* (){
     console.log('do stuff with your id'+this.params.id);
    // for example you can check against the id of the passport user you stored in a database of logged in users ...
    });
    

    如果您使用护照工作人员,您应该考虑令牌而不是 id,因为人们可能知道您的 Facebook id。出于这个原因,令牌是您喜欢用来发送的方式。

    有一个 StackOverflow 问题可以帮助您找到自己的方式: nodejs passport authentication token

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2016-09-01
      • 2019-03-11
      • 2014-06-06
      • 1970-01-01
      • 2011-08-01
      • 2022-01-11
      相关资源
      最近更新 更多