简体   繁体   English

你如何让 Flask-SocketIO 改变 Flask 存储在 cookie 中的客户端会话?

[英]How do you get Flask-SocketIO to alter the client's session stored in a cookie by Flask?

So essentially, what I'm trying to do is remove a user's session id whenever they leave the website/close their tab because I want them to re-login every time they access the site.所以本质上,我想要做的是在用户离开网站/关闭标签时删除用户的会话 ID,因为我希望他们每次访问该网站时都重新登录。 I've heard from other Stack Overflow questions that I should try out the Flask-SocketIO extension, and use the disconnect event to detect when they leave the website and then pop their ids from the session.我从其他 Stack Overflow 问题中听说我应该尝试 Flask-SocketIO 扩展,并使用断开连接事件来检测他们何时离开网站,然后从会话中弹出他们的 id。 So that's exactly what I did, however, whenever I pop the session, it doesn't actually register.所以这正是我所做的,但是,每当我弹出会话时,它实际上并没有注册。 Here's the full code I used to try implement that.这是我用来尝试实现它的完整代码。

# Socket IO Events
@socket_io.on('connect')
def on_connect():
    app.logger.info("Connected!")
    app.logger.info("SESSION INFO: " + str(session))


@socket_io.on('disconnect')
def on_disconnect():
    app.logger.info("Client disconnected!")
    app.logger.info("SESSION INFO: " + str(session))
    if 'id' in session:
        session.pop('id')

So as you can tell, whenever I sign up and head to the home page, I receive a session id, and when this disconnect event fires, my session id gets popped.因此,如您所知,每当我注册并前往主页时,我都会收到一个会话 ID,当此断开连接事件触发时,我的会话 ID 会弹出。 However, take a look at this output.但是,看看这个输出。

[2021-07-30 14:20:39,765] INFO in app: Connected!
[2021-07-30 14:20:39,766] INFO in app: SESSION INFO: {'id': 1}

yjI1iUG5o3YmBgRlAAAA: Sending packet PING data None
9jo8DD7RQ5mEcUWZAAAI: Upgrade to websocket successful
yjI1iUG5o3YmBgRlAAAA: Received packet PONG data
HYTS2jEcmhqp9Vq5AAAE: Sending packet PING data None
KQznMBiop36XZLcTAAAC: Client is gone, closing socket
[2021-07-30 14:20:53,854] INFO in app: Client disconnected!
[2021-07-30 14:20:53,854] INFO in app: SESSION INFO: {}

[2021-07-30 14:21:35,164] INFO in app: Connected!
hBzSZoZ-W7_nesEBAAAK: Received request to upgrade to websocket
[2021-07-30 14:21:35,168] INFO in app: SESSION INFO: {'id': 1}

After connecting to the page, it gives me a session id.连接到页面后,它给了我一个会话 ID。 Then, when I disconnect, it removes my session id and it clearly shows over there, that it removed my session id as there isn't anything in the session dict.然后,当我断开连接时,它会删除我的会话 ID,并在那里清楚地显示,它删除了我的会话 ID,因为会话 dict 中没有任何内容。 However, when I reconnect, it like automatically gives me my session id.但是,当我重新连接时,它就像自动给我我的会话 ID。

Now based on what I've read from this other question on removing session ids, I cannot use socketio to alter the client's cookies, that's at least what I understood from it.现在,根据我从关于删除会话 id 的另一个问题中读到的内容,我不能使用 socketio 来更改客户端的 cookie,至少我是这样理解的。 He also said that it'd be better to store the sessions on the server side.他还说最好将会话存储在服务器端。 But I find that a little troublesome and I don't want to just give up on this.但我觉得这有点麻烦,我不想就此放弃。 Is there any way that I may store client sessions using Flask's built-in sessions system(that store cookies on the client's side), but still allow me to alter them from a socket-io perspective?有什么方法可以使用 Flask 的内置会话系统(在客户端存储 cookie)来存储客户端会话,但仍然允许我从 socket-io 的角度更改它们? I'm just very lost on this.我对此非常迷茫。 Hope someone can explain how socket-io works or just provide a good and comprehensive article on it.希望有人可以解释 socket-io 是如何工作的,或者只是提供一篇关于它的优秀而全面的文章。 Thanks in advance :) But the main problem is, Flask-SocketIO isn't popping the session id when I tell it to and I'm not sure why.在此先感谢 :) 但主要问题是,当我告诉它时 Flask-SocketIO 没有弹出会话 ID,我不知道为什么。

I'm sorry but you have been misled.对不起,你被误导了。 Using Flask-SocketIO just so that you can be notified when a person leaves your site is extremely overkill, and as you've seen, it doesn't even work for your purposes.使用 Flask-SocketIO 只是为了在有人离开您的站点时通知您是非常矫枉过正的,而且正如您所见,它甚至无法满足您的目的。

If you want to know why you can't make changes to the user session from a Socket.IO event handler the reason is that Flask-SocketIO uses WebSocket.如果您想知道为什么不能从 Socket.IO 事件处理程序更改用户会话,原因是 Flask-SocketIO 使用 WebSocket。 The user session is maintained in a cookie, which can only be modified in a server response to an HTTP request initiated by the client.用户会话保存在 cookie 中,它只能在服务器对客户端发起的 HTTP 请求的响应中进行修改。 WebSocket has no ability to modify cookies. WebSocket 无法修改 cookie。

So I would forget about using Flask-SocketIO for this.所以我会忘记为此使用 Flask-SocketIO。 it's really not one of its use cases.它真的不是它的用例之一。

Instead, I suggest you look into adding a beforeunload event in your page, where you can delete the session cookie directly in the client.相反,我建议您考虑在页面中添加beforeunload事件,您可以在其中直接在客户端中删除会话 cookie。 For this to work you will also need to set the SESSION_COOKIE_HTTPONLY configuration option to Flask, so that JavaScript in your page can see, modify and delete the cookie.为此,您还需要将SESSION_COOKIE_HTTPONLY配置选项设置为 Flask,以便您页面中的 JavaScript 可以查看、修改和删除 cookie。 Note that this may have security implications, so think about it very carefully before doing it.请注意,这可能具有安全隐患,因此在执行此操作之前请务必仔细考虑。

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

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