log out a stateless app

让人想犯罪 __ 提交于 2019-12-25 09:05:00

问题


Here is what my project structure look like

  • UI: AngularJS app
  • Backend: Java + MongoDB stateless app

The UI authenticates a given user and the REST api responds with a JWT token. For every subsequent request, the REST api expects token in the header and if its not there it returns Unautorized error.

Now, what is best way to implement logoff feature ? One thing is clear that i will be deleting the token from the UI cookie. But I need to tell server that the user has logged out.

I was thinking to maintain an in-memory db to keep track who has logged in and remove the user from memory once he is logged out BUT it would make the app some sort of stateful (I guess). Also, it could become complicated to scale the app since i would have to replicate the in-memory users across all nodes.

For every REST call, I am fetching the user-details from MongoDB. Would it make sense to use the DB to store the logged in status ? I am just thinking out loud. Since i don't know what directions to head.


回答1:


If you are using JWT's to maintain sessions client-side, then the server should have no concept of logged in and logged out users.

This is the price you pay for deciding to use JWT's (which may be fine of course, depending on your risk appetite for the application).

If you wish to log users out server-side, then you should scrap the JWT model and record sessions server-side. That way you can delete the server-side record as well as the cookie on logout.



来源:https://stackoverflow.com/questions/39238543/log-out-a-stateless-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!