简体   繁体   English

如何将会话中的身份验证数据存储在较低级别?

[英]How does storing authentication data in session work in a low level?

Regardless of a language and framework, how does this work in a low level -- putting a variable into a session to authentication a user? 无论语言和框架如何,这在低级别中如何工作 - 将变量放入会话以对用户进行身份验证?

put_session(curr_connection, :current_user, user.id)

Does user user saved in a cookie? 用户用户是否保存在cookie中? On a client? 在客户端? Then what prevents a user of a browser change it by storing id of any user they desire and get authenticated on behalf on that user? 那么什么阻止浏览器的用户通过存储他们想要的任何用户的ID并代表该用户进行身份验证来改变它? Or does user.id get saved on a server and on a client we have only a loooooong session id , in a cookie or in url? 或者user.id保存在服务器上还是在客户端上我们只有一个loooooong 会话ID ,在cookie或url中?

The short answer is it depends. 简短的答案取决于它。 All languages / frameworks have their defaults, Ruby on Rails for example stores it in a cookie by default, PHP stores it on the server, etc. But in pretty much all of these languages, you can change your cookie store to whatever you want. 所有语言/框架都有它们的默认值,例如Ruby on Rails默认将它存储在cookie中,PHP将它存储在服务器上等等。但是在几乎所有这些语言中,您可以将cookie存储区更改为您想要的任何内容。

Some options (there may be more): 一些选项(可能还有更多):

  • Cookies - In this case the cookie is encrypted before sent to the client. Cookie - 在这种情况下,Cookie会在发送到客户端之前进行加密。 The key used for encryption is some sort of an application setting. 用于加密的密钥是某种应用程序设置。 This is somewhat secure, because even if session values are stored on the client, a user still cannot see or modify them, because he does not have the application key. 这有点安全,因为即使会话值存储在客户端上,用户仍然无法查看或修改它们,因为他没有应用程序密钥。 The advantage of this is that it's very simple and requires zero setup, disadvantages include this being less secure than other solutions, and also the amount of data that can be stored in a cookie is limited. 这样做的好处是它非常简单并且需要零设置,缺点包括这比其他解决方案更不安全,并且可以存储在cookie中的数据量也是有限的。

  • Server memory - In this case, a cryptographically random session id is sent to the client, all session data is stored in the application server memory, identified by the session id. 服务器内存 - 在这种情况下,将加密随机会话ID发送到客户端,所有会话数据都存储在应用程序服务器内存中,由会话ID标识。 The advantage is that it's not written to disk and also not sent to the client. 优点是它不会写入磁盘,也不会发送到客户端。 Disadvantages include the session data being lost when the application server is restarted. 缺点包括重新启动应用程序服务器时会话数据丢失。

  • Server Filesystem - The traditional approach (kind of), session data is stored in files so that it's persisted across application server restarts. 服务器文件系统 - 传统方法(种类),会话数据存储在文件中,以便在应用程序服务器重新启动时保持不变。 In this case, access control to these files is key, but usually taken care of by the language or framework. 在这种情况下,对这些文件的访问控制是关键,但通常由语言或框架来处理。

  • Server SQL Database - The traditional heavy-weight approach, all session data is stored in a relational database on either the application server or a separate database server. 服务器SQL数据库 - 传统的重量级方法,所有会话数据都存储在应用程序服务器或单独的数据库服务器上的关系数据库中。 The advantage is that you have direct control to session contents of any suer, not just the logged on one (for example it's easy to do forced logout for an admin by removing session entries from the database). 优点是您可以直接控制任何suer的会话内容,而不仅仅是登录的内容(例如,通过从数据库中删除会话条目,很容易强制注销管理员)。 This same thing can also be a disadvantage in case of an application level attack. 在应用程序级别攻击的情况下,同样的事情也可能是一个缺点。 Also operation is more expensive. 操作也更昂贵。

  • Server NoSQL Database - About the same as a relational database, but a non-relational database like Redis can also be used. 服务器NoSQL数据库 - 与关系数据库大致相同,但也可以使用像Redis这样的非关系数据库。 One drawback can be that access control in Redis is not very strong to say the least. 一个缺点是Redis中的访问控制至少可以说不是很强。

  • Session Service - In some enterprise applications you may want to implement some kind of a session service (RESTful or else). 会话服务 - 在某些企业应用程序中,您可能希望实现某种会话服务(RESTful或其他)。 Obviously this just pushes the problem one layer back, session data must still be stored somewhere with one of the options above. 显然,这只是将问题推回一层,会话数据仍然必须存储在上面的一个选项中。

Your language or environment probably already supports some of these, and if you want one that is not supported out of the box, you can implement your own. 您的语言或环境可能已经支持其中一些,如果您想要一个不支持开箱即用的语言或环境,您可以实现自己的语言或环境。 However, session management is tricky business, it's quite easy to make it vulnerable. 然而,会话管理是一项棘手的业务,很容易让它变得脆弱。 OWASP has a nice session management cheat sheet to consult. OWASP有一个很好的会话管理备忘单可供参考。

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

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