对于多平台 Web 应用程序来说,什么是好的用户管理实践
我想听听您关于健壮框架/代码(RoR、Zend、Symfony、Django...)使用什么的建议。
我的用户管理系统(了解:登录、注销、用户登录、user_id、用户名等;)使用一个简单而微小的框架(userCake)。我正在重构我的代码,为 iPhone/Androi/Js 使用 API,我想刷新这段代码。我需要知道我是否只是重新排列我的类、依赖项、类并保持相同的流程,或者是否需要更改一些内容。
当前状况
用户成功登录后,我检索他的所有数据,将其序列化,创建一个 sessionID。我将此 sessionID 存储在用户 Cookie 中,并将所有序列化数据存储在链接 sessionID 和数据的数据库表中。
然后,在我的网站上,每次请求页面时,我都会从数据库检索与用户 cookie/会话中的 sessionID 相匹配的序列化数据。然后,我进行查询以确保用户名和散列密码在我的数据库中匹配,并且我的用户仍然处于活动状态(没有被禁止或删除)。我的 api 也是如此(iphone、android、js 必须给我 sessionID)。
我有一个 cronjob 从我的会话表中删除过期的表。当用户注销时,情况相同,我删除数据库中的行并清除用户 Cookie。
我在寻找什么
我上面的解释对我来说似乎很好。但它足够安全吗? 每次用户名和密码都正常时是否真的需要双重数据库检查,我不能只信任会话表中的序列化数据吗?
这一切是如何在著名的框架(Django、Rails、Symfony..)中完成的?
I'd like your advice on what is used by robust frameworks/code (RoR, Zend, Symfony, Django..).
I use for my User Management system (understand: login, logout, is user logged, user_id, username, etc..;) a simple et tiny framework (userCake). I'm refactoring my code, making API for iPhone/Androi/Js use and I'd like to refresh this piece of code. I need to know if I just rearrange my classes, dependencies, classes and keep the same process OR if I need to change some things.
Current state of play
After the user successfully signIn, I retrieve all his data, serialize it, create a sessionID. I store this sessionID in a user Cookie and all the serialized data on a DB table that links the sessionID and the data.
Then, on my website, each time a page is requested, I retrieve from DB the serialized data matching the sessionID the user has in his cookie/session. I then make a query to be sure that the username and the hashed password matches in my db and my user is still active (not banned or removed). The same is made with my api (the iphone, android, js must give me the sessionID).
I've got a cronjob that deletes from my session Table the one which expired. When a user logs out, it's the same, I delete the line in my db and clear the user Cookie.
What I'm looking for
What I explained above seems good to me. But is it secure enough ?
Is it really needed to double DB check each time that all is fine w/ username and password, can't I just trust the serialized data in my session table?
How all that is done in famous frameworks (Django, Rails, Symfony..)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一:
当你“检索他的所有数据”时,这不是已经从数据库中了吗?为什么要序列化数据并将其放回数据库的另一部分?为什么不将用户 ID 存储在会话变量中,并使用它来查询您可能需要的任何用户信息?
第二:
您从哪里获取每个请求的用户名和密码以针对数据库进行测试?听起来好像您正在测试用户,并根据数据库中的原始信息将序列化的存储传递到数据库中。如果是这样,这将始终匹配,因为您自己设置了它。
第三:
为什么使用 cron 作业来删除过期的会话?会话有一个内置的过期机制。
通常,您需要使用指纹识别和/或趋势分析来保护您的会话。然后,您可以简单地将用户的主数据库密钥保存在会话变量中,并使用每个请求来检索您可能需要的任何用户数据。另外,您不需要自己创建会话 ID 并将其存储在 cookie 中。当您调用 session_start() 时,PHP 会为您处理此机制,然后将要保存的任何会话变量存储在 $_SESSION 超全局数组中。您提到的大多数框架都是开源的,因此如果您愿意,您可以查看它们的代码并亲自了解它们的用途。
First:
When you "retrieve all his data" wasn't this from a database already? why would you then serialize the data and put it back into another part of the database? why not just store the user id in a session variable, and use this to query any user info you may need?
Second:
Where are you getting the username and password each request to test against the DB? it sounds as though you are testing the user and pass you store serialized in your db against the original info in the DB. If so, this will always match, as you set this yourself.
Third:
Why do you use a cron job to remove expired sessions? Sessions have a built in expiration mechanism.
Typically, you will want to secure your session using fingerprinting and/or trending. you can then simply save your user's primary db key in a session variable, and use that each request to retrieve any user data you may need. Also, you shouldn't need to create a session id and store it in a cookie yourself. PHP handles this mechanism for you when you call session_start(), and then store any session variables you want to save in the $_SESSION superglobal array. Most of the frameworks you mentioned are open source, so if you want, you can look through their code and see for yourself what they do.