简体   繁体   English

使用Passport与Node-Express和MongoDB进行套接字IO身份验证的案例

[英]A Case of Socket IO authentication using Passport with Node-Express and MongoDB

This is my setup - 这是我的设置-

Three different servers running three different node js application on three different EC2 servers. 三个不同的服务器在三个不同的EC2服务器上运行三个不同的节点js应用程序。

Server 1 - running main application. 服务器1-运行主应用程序。

Server 2 - running admin application. 服务器2-运行管理应用程序。

Server 3 - running service Provider application. 服务器3-正在运行的服务提供程序应用程序。

Each server is using same MongoDB database because they have to share collections. 每个服务器都使用相同的MongoDB数据库,因为它们必须共享集合。

So I have - 所以我有 -

Server 4 - Database server - running Mongo DB. 服务器4-数据库服务器-运行Mongo DB。

In database there are three collections representing three types of users - 在数据库中,有三个表示三种类型用户的集合-

1] Main user 2] Admin user 3] Service Provider 1]主要用户2]管理员用户3]服务提供商

Each type of user logs into his own nodejs application by connecting to respective server using different client applications. 每种类型的用户都通过使用不同的客户端应用程序连接到各自的服务器来登录自己的nodejs应用程序。

For example Admin User connects to Server 2 and logs in the admin Application. 例如,“ 管理员用户”连接到服务器2并登录“管理应用程序”。

For login passport is used, and sessions are stored in Mongo db, using connect-mongo . 对于登录护照,使用session -mongo将会话存储在Mongo db中。 All the sessions of all types of user are stored in same sessions collection in Mongo DB. 所有类型的用户的所有会话都存储在Mongo DB的同一会话集中。

The Problem - 问题 -

Now I want to implement a notification system using socket.io. 现在,我想使用socket.io实现一个通知系统。 Each type of user has a notification Sub-Document array in its model. 每种类型的用户在其模型中都有一个通知子文档数组。 Something like this - 像这样-

Main User schema 主要用户架构

              {
                 ....

                 notifications : [{ text : String , date : Date}]

              }

Admin User schema 管理员用户架构

              {
                 ....

                 notifications : [{ text : String , date : Date}]

              }

Service Provider schema 服务提供商架构

              {
                 ....

                 notifications : [{ text : String , date : Date}]

              }

If a main user books the service of particular service provider, first the service booking info would be stored then a notification would be put in the service providers notifications array. 如果主要用户预订特定服务提供商的服务,则首先将存储服务预订信息,然后在服务提供商通知数组中放置一个通知。 Now if the service provider is online the notification should be pushed to the service provider client application. 现在,如果服务提供商在线,则应该将通知推送到服务提供商客户端应用程序。

For this I am trying to use Socket.io. 为此,我尝试使用Socket.io。

My Approach 我的方法

Setup of a new server for push notifications- 设置用于推送通知的新服务器-

Server 5 - Notification server. 服务器5-通知服务器。

This server runs a socket.io server. 该服务器运行一个socket.io服务器。 When ever any type of user logs in to respective node js application using respective client application, he also connects to this notification server after successful login. 每当任何类型的用户使用相应的客户端应用程序登录到相应的node js应用程序时,他都会在成功登录后连接到该通知服务器。

Now when a MainUser books a service of particular service provider, the main server application would put a notification into that particular service providers notification array in his document. 现在,当MainUser预订特定服务提供商的服务时,主服务器应用程序会将通知放入其文档中的特定服务提供商通知数组。 After that it would send a request to the notification server to emit a notification on channel specified by ID of service provider so that if the service provider is online he receives the notification on his client application instantly. 之后,它将向通知服务器发送请求,以在服务提供商的ID指定的频道上发出通知,以便如果服务提供商在线,他将立即在其客户端应用程序上接收该通知。

But how do I make sure that the users who connects to my socket io server is legitimate. 但是,如何确保连接到我的套接字io服务器的用户合法。 If any user can connect to socket IO server and listen to channel of other user if somehow he gets the ID of other user and get all notification of other user.What I want is the notification server should look at the sessions collection in Mongo DB database and check if the connected user is authenticated and has the proper user id. 如果任何用户可以连接到套接字IO服务器并侦听其他用户的通道(如果以某种方式获得其他用户的ID并获取其他用户的所有通知)。我想要的是通知服务器应查看Mongo DB数据库中的会话集合并检查连接的用户是否已通过身份验证并具有正确的用户ID。

From your post I'm assuming you are using express, express-session and socket.io 从您的帖子中,我假设您正在使用express,express-session和socket.io

You can add a middleware to you socket.io that links your express session to your socket.io socket. 您可以在socket.io中添加一个中间件,该中间件将您的快速会话链接到socket.io套接字。

The expressSessionMiddleware here is an instance of the "express-session" middleware, which should be configured identically to the session middleware in the main application. 这里的expressSessionMiddleware是“ express-session”中间件的实例,应与主应用程序中的会话中间件配置相同。

socketio = require "socket.io"
io       = socketio()

io.use (socket, next) ->
    expressSessionMiddleware socket.handshake, {}, next

You will then have access to the session on socket.handshake.session 然后,您将可以访问socket.handshake.session上的会话

An example module that does this can be found here: https://github.com/xpepermint/socket.io-express-session 可以在此处找到执行此操作的示例模块: https : //github.com/xpepermint/socket.io-express-session

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

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