使用 Express/connectJS 并玩转会话。ConnectJS文档提供了一个简单的视图计数器,这对我有用。
但是,如果我将 cookie 选项设置为“secure:true”,它就会停止工作,就像这样......
app.use(express.session({key:'NodeJS', secret:'cookie', cookie:{maxAge:3600000, secure:true}}));
我看到的是 sessionID 仅在“安全:假”的情况下在请求中发送。如果“安全:真”,我会在每个请求中获得一个新的 sessID。
这失败了。
Req header - SECURE:TRUE
Cookie connect.sid=s%3AP2q10tK255KdVcizJMwETG69.IU2idEzOVaCxahdcRoEGhTkUBK%2B1Evxc0boj97nJgHg;
connect.sess=s%3Aj%3A%7B%7D.opP3ZU2IoVMmmqws%2F5vFGB4s8FNXJ8R6RNZoF4KRNfU
这行得通。
Req header - SECURE:FALSE
Cookie connect.sid=s%3AP2q10tK255KdVcizJMwETG69.IU2idEzOVaCxahdcRoEGhTkUBK%2B1Evxc0boj97nJgHg;
connect.sess=s%3Aj%3A%7B%7D.opP3ZU2IoVMmmqws%2F5vFGB4s8FNXJ8R6RNZoF4KRNfU;
NodeJS=s%3ArBKqb-Wptr_IJMYW6VzJVu__.2Yc3GL32L%2BHuY4L5RxepdiEirtvbSlIeErWVIHXm4uc
有人可以简要解释一下这里发生了什么,或者指出我很好的解释吗?
=============跟进===============
会话选项“安全”使用 http 或 https 切换。
现在,有两种类型的会话管理:会话和cookieSession。以下是每个的输出:
会议
app.use(express.cookieParser());
app.use(express.session({key:'NodeJS', secret:'cookie', cookie:{maxAge:3600000, secure:false}}));
=== req.sessionID ===
b1t73i-MjbyPfQaZHo7_0aGn
==== req.sessionStore ===
{ sessions: { 'b1t73i-MjbyPfQaZHo7_0aGn': '{"cookie":{"originalMaxAge":3600000,"expires":"2013-08-12T19:13:37.169Z","secure":false,"httpOnly":true,"path":"/"},"views":1}' } generate: [Function], _events: { disconnect: [Function], connect: [Function] } }
=== req.session ===
{ cookie: { path: '/', _expires: Mon Aug 12 2013 12:13:37 GMT-0700 (PDT), originalMaxAge: 3600000, httpOnly: true, secure: false }, views: 1 }
==== req.cookies ===
{ NodeJS: 's:b1t73i-MjbyPfQaZHo7_0aGn.RDYYqfFjjAYFry9PmQW/fih+PPMTgNK6uwy5EW372CE', woot: 'woot' }
=== req.signedCookies ===
{ swoot: 'swoot' }
饼干会议
app.use(express.cookieParser());
app.use(express.cookieSession({key:'NodeJS', secret:'cookie', cookie:{maxAge:3600000, secure:false}}));
=== req.sessionID ===
undefined
==== req.sessionStore ===
undefined
=== req.session ===
{ views: 1, cookie: { path: '/', _expires: Mon Aug 12 2013 12:20:16 GMT-0700 (PDT), originalMaxAge: 3600000, httpOnly: true, secure: false } }
==== req.cookies ===
{ NodeJS: 's:j:{"views":1}.JtWQMoqzLEZBoGSxbxXfyqFJ2KIYyJG9GDSY4rxdVLQ', woot: 'woot' }
=== req.signedCookies ===
{ swoot: 'swoot' }
SESSION 返回我可以使用的对象,但我应该如何处理 SESSIONCOOKIE?