migration.adoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. [[migration]]
  2. = Migrating to 6.0
  3. The Spring Security team has prepared the 5.8 release to simplify upgrading to Spring Security 6.0.
  4. Use 5.8 and
  5. ifdef::spring-security-version[]
  6. xref:5.8.0@migration.adoc[its preparation steps]
  7. endif::[]
  8. ifndef::spring-security-version[]
  9. its preparation steps
  10. endif::[]
  11. to simplify updating to 6.0
  12. After updating to 5.8, follow this guide to perform any needed migration steps.
  13. Also, this guide includes ways to <<revert,revert to 5.x>> behaviors and its defaults, should you run into trouble.
  14. == Servlet
  15. 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`].
  16. Saving must be done just prior to the `HttpServletResponse` being committed and just before `SecurityContextPersistenceFilter`.
  17. 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`).
  18. 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.
  19. 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`.
  20. Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
  21. This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
  22. 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.
  23. include::partial$servlet/architecture/security-context-explicit.adoc[]
  24. [[requestcache-query-optimization]]
  25. === Optimize Querying of `RequestCache`
  26. In Spring Security 5, the default behavior is to query the xref:servlet/architecture.adoc#savedrequests[saved request] on every request.
  27. 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.
  28. In Spring Security 6, the default is that `RequestCache` will only be queried for a cached request if the HTTP parameter `continue` is defined.
  29. This allows Spring Security to avoid unnecessarily reading the `HttpSession` with the `RequestCache`.
  30. In Spring Security 5 the default is to use `HttpSessionRequestCache` which will be queried for a cached request on every request.
  31. 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:
  32. include::partial$servlet/architecture/request-cache-continue.adoc[]
  33. === Use `AuthorizationManager` for Method Security
  34. There are no further migration steps for this feature.
  35. However, if you run into trouble with this enhancement, you can instead <<servlet-replace-methodsecurity-with-globalmethodsecurity,revert the behavior>>.
  36. == Reactive
  37. === Use `AuthorizationManager` for Method Security
  38. If you run into trouble with this enhancement, you can instead <<reactive-change-to-useauthorizationmanager-false,revert the behavior>>.
  39. In 6.0, `@EnableReactiveMethodSecurity` defaults `useAuthorizationManager` to `true`.
  40. So, to complete migration, {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] remove the `useAuthorizationManager` attribute:
  41. ====
  42. .Java
  43. [source,java,role="primary"]
  44. ----
  45. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  46. ----
  47. .Kotlin
  48. [source,kotlin,role="secondary"]
  49. ----
  50. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  51. ----
  52. ====
  53. changes to:
  54. ====
  55. .Java
  56. [source,java,role="primary"]
  57. ----
  58. @EnableReactiveMethodSecurity
  59. ----
  60. .Kotlin
  61. [source,kotlin,role="secondary"]
  62. ----
  63. @EnableReactiveMethodSecurity
  64. ----
  65. ====
  66. '''
  67. [[revert]]
  68. If you are running into trouble with any of the 6.0 changes, please first try to apply the following changes to get you up and running.
  69. It's more important to stay on 6.0 and get the security improvements.
  70. == Revert Servlet
  71. [[servlet-replace-methodsecurity-with-globalmethodsecurity]]
  72. === Don't Use `AuthorizationManager` in Method Security
  73. To opt out of `AuthorizationManager` for Method Security, replace xref:servlet/authorization/method-security.adoc#jc-enable-method-security[method security] with xref:servlet/authorization/method-security.adoc#jc-enable-global-method-security[global method security]
  74. For applications using xref:servlet/authorization/method-security.adoc#jc-enable-method-security[pre-post annotations], make sure to turn it on to reactivate the behavior.
  75. For example, change:
  76. ====
  77. .Java
  78. [source,java,role="primary"]
  79. ----
  80. @EnableMethodSecurity
  81. ----
  82. .Kotlin
  83. [source,kotlin,role="secondary"]
  84. ----
  85. @EnableMethodSecurity
  86. ----
  87. .Xml
  88. [source,xml,role="secondary"]
  89. ----
  90. <method-security/>
  91. ----
  92. ====
  93. to:
  94. ====
  95. .Java
  96. [source,java,role="primary"]
  97. ----
  98. @EnableGlobalMethodSecurity(prePostEnabled = true)
  99. ----
  100. .Kotlin
  101. [source,kotlin,role="secondary"]
  102. ----
  103. @EnableGlobalMethodSecurity(prePostEnabled = true)
  104. ----
  105. .Xml
  106. [source,xml,role="secondary"]
  107. ----
  108. <global-method-security pre-post-enabled="true"/>
  109. ----
  110. ====
  111. Other usages can simply change {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-method-security[`<method-security>`] to {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableGlobalMethodSecurity.html[`@EnableGlobalMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-global-method-security[`<global-method-security>`], like so:
  112. ====
  113. .Java
  114. [source,java,role="primary"]
  115. ----
  116. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  117. ----
  118. .Kotlin
  119. [source,kotlin,role="secondary"]
  120. ----
  121. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  122. ----
  123. .Xml
  124. [source,xml,role="secondary"]
  125. ----
  126. <method-security secured-enabled="true" pre-post-enabled="false"/>
  127. ----
  128. ====
  129. should change to:
  130. ====
  131. .Java
  132. [source,java,role="primary"]
  133. ----
  134. @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = false)
  135. ----
  136. .Kotlin
  137. [source,kotlin,role="secondary"]
  138. ----
  139. @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = false)
  140. ----
  141. .Xml
  142. [source,xml,role="secondary"]
  143. ----
  144. <global-method-security secured-enabled="true" pre-post-enabled="false"/>
  145. ----
  146. ====
  147. == Revert Reactive
  148. [[reactive-change-to-useauthorizationmanager-false]]
  149. === Don't Use `AuthorizationManager` in Method Security
  150. To opt-out of {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] for reactive method security, add `useAuthorizationManager = false`:
  151. ====
  152. .Java
  153. [source,java,role="primary"]
  154. ----
  155. @EnableReactiveMethodSecurity
  156. ----
  157. .Kotlin
  158. [source,kotlin,role="secondary"]
  159. ----
  160. @EnableReactiveMethodSecurity
  161. ----
  162. ====
  163. changes to:
  164. ====
  165. .Java
  166. [source,java,role="primary"]
  167. ----
  168. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  169. ----
  170. .Kotlin
  171. [source,kotlin,role="secondary"]
  172. ----
  173. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  174. ----
  175. ====