管理Session的通用基础设施:Spring Session

jopen 9年前

Spring Session目的是为管理Session提供一个通用基础设施。它具有以下优点/好处:

  • 从任意环境访问同一个Session(比如:web, messaging infrastructure等)

  • 在一个web环境中:

    • 以一种与供应者(vendor)无关的集群支持

    • 可插拔的策略来判断会话ID

    • 轻松保持HttpSession可用,当一个WebSocket活动时

    </li> </ul> </div> </div>

    具体的优点/好处:

    • This can make clustering much easier. This is nice because the clustering setup is done in a vendor neutral way. Furthermore, in some environments (i.e. PaaS solutions) developers cannot modify the cluster settings easily.

    • We can use different strategies for determining the session id. This gives us at least a few benefits

      • Allowing for a single browser to have multiple simultaneous sessions in a transparent fashion. For example, many developers wish to allow a user to authenticate with multiple accounts and switch between them similar to how you can in gmail.

      • When using a REST API, the session can be specified using a header instead of the JSESSIONID cookie (which leaks implementation details to the client). Many would argue that session is bad in REST because it has state, but it is important to note that session is just a form of cache and used responsibly it will increase performance & security.

      • When a session id is acquired in a header, we can default CSRF protection to off. This is because if the session id is found in the header we know that it is impossible to be a CSRF attack since, unlike cookies, headers must be manually populated.

      </li>
    • We can easily keep the HttpSession and WebSocket Session in sync. Imagine a web application like gmail where you can authenticate and either write emails (HTTP requests) or chat (WebSocket). In standard servlet environment there is no way to keep the HttpSession alive through the WebSocket so you must ping the server. With our own session strategy we can have the WebSocket messages automatically keep the HttpSession alive. We can also destroy both sessions at once easily.

    • We can provide hooks to allow users to invalidate sessions that should not be active. For example, if you look in the lower right of gmail you can see the last account activity and click "Details". This shows a listing of all the active sessions along with the IP address, location, and browser information for your account.

      • Users can look through this and determine if anything is suspicious (i.e. if their account has a session that is associated to a country they have never been) and invalidate that session and change their password.

      • Another useful example is perhaps they checked their mail at the library and forgot to log out. With this custom mechanism this is very possible.

      • </ul> </div> </li>
      • Spring Security currently supports restricting the number of concurrent sessions each user can have. The implementation works, but does so passively since we cannot get a handle to the session from the session id. Specifically, each time a user requests a page we check to see if that session id is valid in a separate data store. If it is no longer valid, we invalidate the session. With this new mechanism we can invalidate the session from the session id.

      • </ul> </div>

        项目主页:http://www.open-open.com/lib/view/home/1416358563445