migration.adoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. === Use `AuthorizationManager` for Message Security
  37. In 6.0, `<websocket-message-broker>` defaults `use-authorization-manager` to `true`.
  38. So, to complete migration, remove any `websocket-message-broker@use-authorization-manager=true` attribute.
  39. For example:
  40. ====
  41. .Xml
  42. [source,xml,role="primary"]
  43. ----
  44. <websocket-message-broker use-authorization-manager="true"/>
  45. ----
  46. ====
  47. changes to:
  48. ====
  49. .Xml
  50. [source,xml,role="primary"]
  51. ----
  52. <websocket-message-broker/>
  53. ----
  54. ====
  55. There are no further migrations steps for Java or Kotlin for this feature.
  56. == Reactive
  57. === Use `AuthorizationManager` for Method Security
  58. If you run into trouble with this enhancement, you can instead <<reactive-change-to-useauthorizationmanager-false,revert the behavior>>.
  59. In 6.0, `@EnableReactiveMethodSecurity` defaults `useAuthorizationManager` to `true`.
  60. So, to complete migration, {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] remove the `useAuthorizationManager` attribute:
  61. ====
  62. .Java
  63. [source,java,role="primary"]
  64. ----
  65. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  66. ----
  67. .Kotlin
  68. [source,kotlin,role="secondary"]
  69. ----
  70. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  71. ----
  72. ====
  73. changes to:
  74. ====
  75. .Java
  76. [source,java,role="primary"]
  77. ----
  78. @EnableReactiveMethodSecurity
  79. ----
  80. .Kotlin
  81. [source,kotlin,role="secondary"]
  82. ----
  83. @EnableReactiveMethodSecurity
  84. ----
  85. ====
  86. '''
  87. [[revert]]
  88. 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.
  89. It's more important to stay on 6.0 and get the security improvements.
  90. == Revert Servlet
  91. [[servlet-replace-methodsecurity-with-globalmethodsecurity]]
  92. === Don't Use `AuthorizationManager` in Method Security
  93. 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]
  94. 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.
  95. For example, change:
  96. ====
  97. .Java
  98. [source,java,role="primary"]
  99. ----
  100. @EnableMethodSecurity
  101. ----
  102. .Kotlin
  103. [source,kotlin,role="secondary"]
  104. ----
  105. @EnableMethodSecurity
  106. ----
  107. .Xml
  108. [source,xml,role="secondary"]
  109. ----
  110. <method-security/>
  111. ----
  112. ====
  113. to:
  114. ====
  115. .Java
  116. [source,java,role="primary"]
  117. ----
  118. @EnableGlobalMethodSecurity(prePostEnabled = true)
  119. ----
  120. .Kotlin
  121. [source,kotlin,role="secondary"]
  122. ----
  123. @EnableGlobalMethodSecurity(prePostEnabled = true)
  124. ----
  125. .Xml
  126. [source,xml,role="secondary"]
  127. ----
  128. <global-method-security pre-post-enabled="true"/>
  129. ----
  130. ====
  131. 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:
  132. ====
  133. .Java
  134. [source,java,role="primary"]
  135. ----
  136. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  137. ----
  138. .Kotlin
  139. [source,kotlin,role="secondary"]
  140. ----
  141. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  142. ----
  143. .Xml
  144. [source,xml,role="secondary"]
  145. ----
  146. <method-security secured-enabled="true" pre-post-enabled="false"/>
  147. ----
  148. ====
  149. should change to:
  150. ====
  151. .Java
  152. [source,java,role="primary"]
  153. ----
  154. @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = false)
  155. ----
  156. .Kotlin
  157. [source,kotlin,role="secondary"]
  158. ----
  159. @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = false)
  160. ----
  161. .Xml
  162. [source,xml,role="secondary"]
  163. ----
  164. <global-method-security secured-enabled="true" pre-post-enabled="false"/>
  165. ----
  166. ====
  167. == Revert Reactive
  168. [[reactive-change-to-useauthorizationmanager-false]]
  169. === Don't Use `AuthorizationManager` in Method Security
  170. To opt-out of {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] for reactive method security, add `useAuthorizationManager = false`:
  171. ====
  172. .Java
  173. [source,java,role="primary"]
  174. ----
  175. @EnableReactiveMethodSecurity
  176. ----
  177. .Kotlin
  178. [source,kotlin,role="secondary"]
  179. ----
  180. @EnableReactiveMethodSecurity
  181. ----
  182. ====
  183. changes to:
  184. ====
  185. .Java
  186. [source,java,role="primary"]
  187. ----
  188. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  189. ----
  190. .Kotlin
  191. [source,kotlin,role="secondary"]
  192. ----
  193. @EnableReactiveMethodSecurity(useAuthorizationManager = false)
  194. ----
  195. ====