dongyong1400 2018-03-22 00:14
浏览 193
已采纳

使用localStorage和Websocket自动授权

I have a project I'm working on which is a single-page dynamic web app which uses Javascript and a Websocket to retrieve data from a Golang server. I've implemented the auto-authorization method described in this article. I was referred to that article from this one which at the end mentions to check out the first article I linked (just in case you were wondering what that first article was "improving" upon).

This is how my setup works:

  • Client loads app, and before anything dynamic loads, client connects to Websocket
  • Client sends login, device token, and session token through Websocket (of course if they were logged in before) from localStorage
  • Golang Websocket server checks if database has matching login, device token, session token, client's browser/version, etc (I get a few other pieces of data to match a device with the user's HTTP request for extra security)
  • If there's a match, Golang generates a new session token, updates the database match with it, and sends it back to the client
  • Client gets session token and updates it in their localStorage to use the next time they load the site

Now, don't get me wrong. Everything is working great... but there's one problem:

If a user logs in, then reloads the page a bunch of times as fast as they can, sometimes the websocket and server don't have enough time to get the user's next session token back to them. This of course causes their tokens to expire and be removed from the database and notifies the user that their credentials have either been expired or stolen (which really doesn't look good from a business standpoint).

I have some thoughts on solutions, but none seem promising in scalability and for the long run.

All thoughts and solutions on the matter are appreciated!

  • 写回答

1条回答 默认 最新

  • dongle7882 2018-03-22 13:58
    关注

    After login you should send two tokens to the browser, a short-lived session token (say, one hour) and a long-lived remember-me token (say, 30 days). The session token's TTL is reset to one hour every time the browser shows activity, but its value does not change.

    If and only if the session cookie has expired (or is otherwise invalid), the remember-me cookie is traded for a new, authenticated, session. This does not happen when reloading the page, only after the browser has been inactive for more than an hour.

    Prolonging the session happens purely on the server side. Only when trading the remember-me token is it critical that the browser receives and stores the new session token.

    You should not delete the remember-me token from the database immediately after first use. If I start my browser it restores previously open tabs, which may include multiple for your site. Consequently, your server will receive multiple requests (or websocket connections) almost simultaneously and those requests race for the new session-token.

    At my workplace we allow remember-me tokens to be used for up to two minutes after first use, and at most N times, whichever happens first. That fixes races and allows for retries in case of connectivity issues.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能