migration.adoc 7.6 KB

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