migration.adoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 the steps below to minimize changes when
  5. ifdef::spring-security-version[]
  6. xref:6.0.0@migration.adoc[updating to 6.0]
  7. endif::[]
  8. ifndef::spring-security-version[]
  9. updating to 6.0
  10. endif::[]
  11. .
  12. == Servlet
  13. [[requestcache-query-optimization]]
  14. === Optimize Querying of `RequestCache`
  15. In Spring Security 5, the default behavior is to query the xref:servlet/architecture.adoc#savedrequests[saved request] on every request.
  16. 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.
  17. In Spring Security 6, the default is that `RequestCache` will only be queried for a cached request if the HTTP parameter `continue` is defined.
  18. This allows Spring Security to avoid unnecessarily reading the `HttpSession` with the `RequestCache`.
  19. In Spring Security 5 the default is to use `HttpSessionRequestCache` which will be queried for a cached request on every request.
  20. 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:
  21. include::partial$servlet/architecture/request-cache-continue.adoc[]
  22. === Use `AuthorizationManager` for Method Security
  23. xref:servlet/authorization/method-security.adoc[Method Security] has been xref:servlet/authorization/method-security.adoc#jc-enable-method-security[simplified] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
  24. '''
  25. [[servlet-replace-globalmethodsecurity-with-methodsecurity]]
  26. ==== Replace xref:servlet/authorization/method-security.adoc#jc-enable-global-method-security[global method security] with xref:servlet/authorization/method-security.adoc#jc-enable-method-security[method security]
  27. {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>`] are deprecated in favor of {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>`], respectively.
  28. The new annotation and XML element activate Spring's xref:servlet/authorization/method-security.adoc#jc-enable-method-security[pre-post annotations] by default and use `AuthorizationManager` internally.
  29. This means that the following two listings are functionally equivalent:
  30. ====
  31. .Java
  32. [source,java,role="primary"]
  33. ----
  34. @EnableGlobalMethodSecurity(prePostEnabled = true)
  35. ----
  36. .Kotlin
  37. [source,kotlin,role="secondary"]
  38. ----
  39. @EnableGlobalMethodSecurity(prePostEnabled = true)
  40. ----
  41. .Xml
  42. [source,xml,role="secondary"]
  43. ----
  44. <global-method-security pre-post-enabled="true"/>
  45. ----
  46. ====
  47. and:
  48. ====
  49. .Java
  50. [source,java,role="primary"]
  51. ----
  52. @EnableMethodSecurity
  53. ----
  54. .Kotlin
  55. [source,kotlin,role="secondary"]
  56. ----
  57. @EnableMethodSecurity
  58. ----
  59. .Xml
  60. [source,xml,role="secondary"]
  61. ----
  62. <method-security/>
  63. ----
  64. ====
  65. For applications not using the pre-post annotations, make sure to turn it off to avoid activating unwanted behavior.
  66. For example, a listing like:
  67. ====
  68. .Java
  69. [source,java,role="primary"]
  70. ----
  71. @EnableGlobalMethodSecurity(securedEnabled = true)
  72. ----
  73. .Kotlin
  74. [source,kotlin,role="secondary"]
  75. ----
  76. @EnableGlobalMethodSecurity(securedEnabled = true)
  77. ----
  78. .Xml
  79. [source,xml,role="secondary"]
  80. ----
  81. <global-method-security secured-enabled="true"/>
  82. ----
  83. ====
  84. should change to:
  85. ====
  86. .Java
  87. [source,java,role="primary"]
  88. ----
  89. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  90. ----
  91. .Kotlin
  92. [source,kotlin,role="secondary"]
  93. ----
  94. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  95. ----
  96. .Xml
  97. [source,xml,role="secondary"]
  98. ----
  99. <method-security secured-enabled="true" pre-post-enabled="false"/>
  100. ----
  101. ====
  102. '''
  103. [[servlet-replace-permissionevaluator-bean-with-methodsecurityexpression-handler]]
  104. ==== Publish a `MethodSecurityExpressionHandler` instead of a `PermissionEvaluator`
  105. `@EnableMethodSecurity` does not pick up a `PermissionEvaluator`.
  106. This helps keep its API simple.
  107. If you have a custom {security-api-url}org/springframework/security/access/PermissionEvaluator.html[`PermissionEvaluator`] `@Bean`, please change it from:
  108. ====
  109. .Java
  110. [source,java,role="primary"]
  111. ----
  112. @Bean
  113. static PermissionEvaluator permissionEvaluator() {
  114. // ... your evaluator
  115. }
  116. ----
  117. .Kotlin
  118. [source,kotlin,role="secondary"]
  119. ----
  120. companion object {
  121. @Bean
  122. fun permissionEvaluator(): PermissionEvaluator {
  123. // ... your evaluator
  124. }
  125. }
  126. ----
  127. ====
  128. to:
  129. ====
  130. .Java
  131. [source,java,role="primary"]
  132. ----
  133. @Bean
  134. static MethodSecurityExpressionHandler expressionHandler() {
  135. var expressionHandler = new DefaultMethodSecurityExpressionHandler();
  136. expressionHandler.setPermissionEvaluator(myPermissionEvaluator);
  137. return expressionHandler;
  138. }
  139. ----
  140. .Kotlin
  141. [source,kotlin,role="secondary"]
  142. ----
  143. companion object {
  144. @Bean
  145. fun expressionHandler(): MethodSecurityExpressionHandler {
  146. val expressionHandler = DefaultMethodSecurityExpressionHandler
  147. expressionHandler.setPermissionEvaluator(myPermissionEvaluator)
  148. return expressionHandler
  149. }
  150. }
  151. ----
  152. ====
  153. '''
  154. [[servlet-check-for-annotationconfigurationexceptions]]
  155. ==== Check for ``AnnotationConfigurationException``s
  156. `@EnableMethodSecurity` and `<method-security>` activate stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  157. If after moving to either you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
  158. == Reactive
  159. === Use `AuthorizationManager` for Method Security
  160. xref:reactive/authorization/method.adoc[Method Security] has been xref:reactive/authorization/method.adoc#jc-enable-reactive-method-security-authorization-manager[improved] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
  161. '''
  162. [[reactive-change-to-useauthorizationmanager]]
  163. ==== Change `useAuthorizationManager` to `true`
  164. In Spring Security 5.8, `useAuthorizationManager` was added to {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] to allow applications to opt-in to ``AuthorizationManager``'s features.
  165. To opt in, change `useAuthorizationManager` to `true` like so:
  166. ====
  167. .Java
  168. [source,java,role="primary"]
  169. ----
  170. @EnableReactiveMethodSecurity
  171. ----
  172. .Kotlin
  173. [source,kotlin,role="secondary"]
  174. ----
  175. @EnableReactiveMethodSecurity
  176. ----
  177. ====
  178. changes to:
  179. ====
  180. .Java
  181. [source,java,role="primary"]
  182. ----
  183. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  184. ----
  185. .Kotlin
  186. [source,kotlin,role="secondary"]
  187. ----
  188. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  189. ----
  190. ====
  191. [NOTE]
  192. =====
  193. In 6.0, `useAuthorizationManager` defaults to `true`.
  194. =====
  195. '''
  196. [[reactive-check-for-annotationconfigurationexceptions]]
  197. ==== Check for ``AnnotationConfigurationException``s
  198. `useAuthorizationManager` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  199. If after turning on `useAuthorizationManager` you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.