简体   繁体   English

Socket.io,Passport,MongoDB和password.socketio在生产中损坏

[英]Socket.io, Passport, MongoDB & passport.socketio broken in production

I built a nodejs application relaying on socket.io, using Passport & passport.socketio for authentication and authorization, and mongodb with connect-mongo for session store. 我建立了一个在socket.io上中继的nodejs应用程序,使用Passport和passport.socketio进行身份验证和授权,使用mongodb和connect-mongo进行会话存储。

It works well on my laptop, but when I moved to the Cloud (Azure-VM) I started getting strange errors. 它可以在笔记本电脑上很好地工作,但是当我移到云(Azure-VM)时,我开始遇到奇怪的错误。

05-02-2014, 11:47:06.500 Listening on port 8081 (https) 2014年5月2日,11:47:06.500在端口8081(https)上侦听

/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/connection/base.js:242
    throw message;      
          ^
Error: Error in session store:
Error: failed to deserialize user out of session
    at Object.io.set.passportSocketIo.authorize.fail     (/home/azureuser/myapp/lib/express/socketio.js:25:23)
    at /home/azureuser/myapp/node_modules/passport.socketio/lib/index.js:48:21
    at /home/azureuser/myapp/node_modules/connect-mongo/lib/connect-mongo.js:229:23
    at /home/azureuser/myapp/node_modules/mongodb/lib/mongodb/collection/query.js:147:5
    at Cursor.nextObject (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/cursor.js:733:5)
    at commandHandler (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/cursor.js:713:14)
    at /home/azureuser/myapp/node_modules/mongodb/lib/mongodb/db.js:1806:9
    at Server.Base._callHandler (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/connection/base.js:442:41)
    at /home/azureuser/myapp/node_modules/mongodb/lib/mongodb/connection/server.js:485:18
    at MongoReply.parseBody (/home/azureuser/myapp/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)

Any ideas where to start? 任何想法从哪里开始?

ok I found it, passport.socketio has its depends on Passport and has it's own "version" of passport. 好的,我发现了,passport.socketio依赖于Passport,并且具有自己的护照“版本”。 Which means that in my code when I set serializeUser/deserializeUser it only affects the passport I use for the REST: 这意味着在我的代码中,当我设置serializeUser / deserializeUser时,它仅影响我用于REST的护照:

passport.serializeUser(function(user, done) {
    done(null, user);
});
passport.deserializeUser(function(id, done) {
    done(null, id);
}); 

While passport.socketio by default does: 默认情况下,passport.socketio会:

var defaults = {
  passport:     require('passport'),
  key:          'connect.sid',
  secret:       null,
  ...
};

Meaning the serializeUser/deserializeUser are not used, which in turn causes this: 这意味着未使用serializeUser / deserializeUser,这又导致以下情况:

Error: failed to deserialize user out of session

The solution is pretty simple just to pass the passport used for the REST to the passport.socketio 该解决方案非常简单,只需将用于REST的护照传递给password.socketio

io.set('authorization', passportSocketIo.authorize({
    passport : passport,
    cookieParser: express.cookieParser,
    ...
 }));

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

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