2
0

session-management.adoc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. = Session Management Migrations
  2. The following steps relate to how to finish migrating session management support.
  3. == Require Explicit Saving of SecurityContextRepository
  4. In Spring Security 5, the default behavior is for the xref:servlet/authentication/architecture.adoc#servlet-authentication-securitycontext[`SecurityContext`] to automatically be saved to the xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] using the xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersistenceFilter`].
  5. Saving must be done just prior to the `HttpServletResponse` being committed and just before `SecurityContextPersistenceFilter`.
  6. Unfortunately, automatic persistence of the `SecurityContext` can surprise users when it is done prior to the request completing (i.e. just prior to committing the `HttpServletResponse`).
  7. It also is complex to keep track of the state to determine if a save is necessary causing unnecessary writes to the `SecurityContextRepository` (i.e. `HttpSession`) at times.
  8. In Spring Security 6, the default behavior is that the xref:servlet/authentication/persistence.adoc#securitycontextholderfilter[`SecurityContextHolderFilter`] will only read the `SecurityContext` from `SecurityContextRepository` and populate it in the `SecurityContextHolder`.
  9. Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
  10. This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
  11. If you are explicitly opting into Spring Security 6's new defaults, the following configuration can be removed to accept the Spring Security 6 defaults.
  12. include::partial$servlet/architecture/security-context-explicit.adoc[]
  13. == Multiple SecurityContextRepository
  14. In Spring Security 5, the default xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] was `HttpSessionSecurityContextRepository`.
  15. In Spring Security 6, the default `SecurityContextRepository` is `DelegatingSecurityContextRepository`.
  16. If you configured the `SecurityContextRepository` only for the purpose of updating to 6.0, you can remove it completely.
  17. == Deprecation in SecurityContextRepository
  18. There are no further migration steps for this deprecation.
  19. [[requestcache-query-optimization]]
  20. == Optimize Querying of `RequestCache`
  21. In Spring Security 5, the default behavior is to query the xref:servlet/architecture.adoc#savedrequests[saved request] on every request.
  22. This means that in a typical setup, that in order to use the xref:servlet/architecture.adoc#requestcache[`RequestCache`] the `HttpSession` is queried on every request.
  23. In Spring Security 6, the default is that `RequestCache` will only be queried for a cached request if the HTTP parameter `continue` is defined.
  24. This allows Spring Security to avoid unnecessarily reading the `HttpSession` with the `RequestCache`.
  25. In Spring Security 5 the default is to use `HttpSessionRequestCache` which will be queried for a cached request on every request.
  26. If you are not overriding the defaults (i.e. using `NullRequestCache`), then the following configuration can be used to explicitly opt into the Spring Security 6 behavior in Spring Security 5.8:
  27. include::partial$servlet/architecture/request-cache-continue.adoc[]