Socket.IO 身份验证
我正在尝试在 Node.js 中使用 Socket.IO,并尝试允许服务器为每个 Socket.IO 客户端提供身份。由于套接字代码超出了 http 服务器代码的范围,因此它无法轻松访问发送的请求信息,因此我假设需要在连接期间发送它。 最好的方法是什么?
1)向服务器获取有关谁通过 Socket.IO 连接的信息
2)验证他们所说的身份(我目前正在使用 Express,如果这会让事情变得更容易),
I am trying to use Socket.IO in Node.js, and am trying to allow the server to give an identity to each of the Socket.IO clients. As the socket code is outside the scope of the http server code, it doesn't have easy access to the request information sent, so I'm assuming it will need to be sent up during the connection. What is the best way to
1) get the information to the server about who is connecting via Socket.IO
2) authenticate who they say they are (I'm currently using Express, if that makes things any easier)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 connect-redis 并将 redis 作为所有经过身份验证的用户的会话存储。确保在身份验证时将密钥(通常为 req.sessionID)发送到客户端。让客户端将此密钥存储在 cookie 中。
在套接字连接时(或以后的任何时候)从 cookie 中获取此密钥并将其发送回服务器。使用这个key获取redis中的session信息。 (GET key)
例如:
服务器端(使用 redis 作为会话存储):
客户端:
服务器端:
Use connect-redis and have redis as your session store for all authenticated users. Make sure on authentication you send the key (normally req.sessionID) to the client. Have the client store this key in a cookie.
On socket connect (or anytime later) fetch this key from the cookie and send it back to the server. Fetch the session information in redis using this key. (GET key)
Eg:
Server side (with redis as session store):
Client side:
Server side:
我也喜欢 pusherapp 的方式 私人频道。
因为每个套接字都有唯一的 socket_id。
他们使用签名授权字符串来授权用户。
我还没有将其镜像到
socket.io
,但我认为这可能是一个非常有趣的概念。I also liked the way pusherapp does private channels.
Because also
socket.io
has unique socket_id for every socket.They used signed authorization strings to authorize users.
I haven't yet mirrored this to
socket.io
, but I think it could be pretty interesting concept.我知道这有点老了,但对于未来的读者来说,除了解析 cookie 和从存储中检索会话的方法之外(例如 passport.socketio )您也可以考虑基于令牌的方法。
在此示例中,我使用相当标准的 JSON Web 令牌。您必须向客户端页面提供令牌,在本例中想象一个返回 JWT 的身份验证端点:
现在,您的 socket.io 服务器可以配置如下:
socket.io-jwt 中间件需要查询字符串中的令牌,因此,从客户端,您只需在连接时附加它:
我写了有关此方法和 cookies 的更详细说明 此处。
I know this is bit old, but for future readers in addition to the approach of parsing cookie and retrieving the session from the storage (eg. passport.socketio ) you might also consider a token based approach.
In this example I use JSON Web Tokens which are pretty standard. You have to give to the client page the token, in this example imagine an authentication endpoint that returns JWT:
Now, your socket.io server can be configured as follows:
The socket.io-jwt middleware expects the token in a query string, so from the client you only have to attach it when connecting:
I wrote a more detailed explanation about this method and cookies here.
这是我尝试进行以下工作:
因为您可能想要添加一些 API 请求,我们还将使用 http 包让 HTTP 和 Web 套接字在同一端口上工作。
server.js
以下摘录仅包含设置先前技术所需的所有内容。您可以在此处查看我在我的一个项目中使用的完整 server.js 版本。
Here is my attempt to have the following working:
Since you might want to add some API requests as well, we'll also use http package to have both HTTP and Web socket working in the same port.
server.js
The following extract only includes everything you need to set the previous technologies up. You can see the complete server.js version which I used in one of my projects here.
sockets/index.js
Our
socketConnectionHandler
, I just don't like putting everything inside server.js (even though you perfectly could), especially since this file can end up containing quite a lot of code pretty quickly.Extra material (client):
Just a very basic version of what the JavaScript socket.io client could be:
References:
I just couldn't reference inside the code, so I moved it here.
1: How to set up your Passport strategies: https://scotch.io/tutorials/easy-node-authentication-setup-and-local#handling-signupregistration
本文(http://simplapi .wordpress.com/2012/04/13/php-and-node-js-session-share-redi/)展示了如何
使用此代码您也可以在socket.io中获取它们。
This article (http://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/) shows how to
Using this code you are able to get them in socket.io, too.
c/s之间使用session和Redis
服务器端
use session and Redis between c/s
Server side
这应该可以做到
this should do it