错误:无法在没有secret或encryptionKey / signatureKey对的情况下设置会话

时间:2018-04-11 17:39:25

标签: node.js ubuntu

我试图让客户端会话在Ubuntu上工作。但是,每当我运行<div> { this.state.person.map(el => ( <Person key={el.name} name={el.name} age={el.age} /> ) ) } </div> 时,我都会收到此错误。我试图找出正在发生的事情,但我无法找到正在发生的事情。我在他们的NPM / Github网站上阅读了客户端会话信息,但我无法弄清楚它们正在发生什么。任何人都可以帮助我或带我到正确的地方吗?

整个错误:

nodejs app.js

2 个答案:

答案 0 :(得分:3)

要使用client-sessions,您必须设置secretencryptionKeysignatureKeyrecommended in the documentation

https://www.npmjs.com/package/client-sessions#usage

var sessions = require("client-sessions");
app.use(sessions({
  cookieName: 'mySession', // cookie name dictates the key name added to the request object 
  secret: 'blargadeeblargblarg', // should be a large unguessable string 
  duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms 
  activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds 
}));

app.use(function(req, res, next) {
  if (req.mySession.seenyou) {
    res.setHeader('X-Seen-You', 'true');
  } else {
    // setting a property will automatically cause a Set-Cookie response 
    // to be sent 
    req.mySession.seenyou = true;
    res.setHeader('X-Seen-You', 'false');
  }
});

code of lib/client-sessions.js检查秘密或两个密钥是否以clientSessionFactory方法初始化:

https://github.com/mozilla/node-client-sessions/blob/d0c20af3b0ed7750c68d3ae67819dfe203fa3d60/lib/client-sessions.js#L542

  if (!(opts.secret || (opts.encryptionKey && opts.signatureKey))) {
    throw new Error("cannot set up sessions without a secret "+
                    "or encryptionKey/signatureKey pair");
  }

https://hacks.mozilla.org/2012/12/using-secure-client-side-sessions-to-build-simple-and-scalable-node-js-applications-a-node-js-holiday-season-part-3/页面说明了如何设置secret - 使用一些长的随机字符串(for example, combine several strings from the site random.org):

app.use(clientSessions({
  secret: '0GBlJZ9EKBt2Zbi2flRPvztczCewBxXK' // set this to a long random string!
}));

答案 1 :(得分:0)

我认为您所犯的错误是不言自明的。如果您遵循堆栈跟踪,您可以在以下路径和行中看到:

/home/tom/cookiestut/app.js:34:9您错过了设置Cookie会话配置的秘密。您可能已将其配置为环境变量,而您忘记定义它。

手动添加密码到cookie设置