migration.adoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. === Change `@EnableGlobalMethodSecurity` to `@EnableMethodSecurity`
  7. 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.
  8. The public API difference between these two annotations is that {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`] defaults `prePostEnabled` to `true`, while {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableGlobalMethodSecurity.html[`@EnableGlobalMethodSecurity`] defaults it to `false`.
  9. Also, `@EnableMethodSecurity` internally uses `AuthorizationManager` while `@EnableGlobalMethodSecurity` does not.
  10. This means that the following two listings are functionally equivalent:
  11. ====
  12. .Java
  13. [source,java,role="primary"]
  14. ----
  15. @EnableGlobalMethodSecurity(prePostEnabled = true)
  16. ----
  17. .Kotlin
  18. [source,kotlin,role="secondary"]
  19. ----
  20. @EnableGlobalMethodSecurity(prePostEnabled = true)
  21. ----
  22. ====
  23. changes to:
  24. ====
  25. .Java
  26. [source,java,role="primary"]
  27. ----
  28. @EnableMethodSecurity
  29. ----
  30. .Kotlin
  31. [source,kotlin,role="secondary"]
  32. ----
  33. @EnableMethodSecurity
  34. ----
  35. ====
  36. For applications not using `prePostEnabled`, make sure to turn it off to avoid activating unwanted behavior.
  37. For example, a listing like:
  38. ====
  39. .Java
  40. [source,java,role="primary"]
  41. ----
  42. @EnableGlobalMethodSecurity(securedEnabled = true)
  43. ----
  44. .Kotlin
  45. [source,kotlin,role="secondary"]
  46. ----
  47. @EnableGlobalMethodSecurity(securedEnabled = true)
  48. ----
  49. ====
  50. should change to:
  51. ====
  52. .Java
  53. [source,java,role="primary"]
  54. ----
  55. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  56. ----
  57. .Kotlin
  58. [source,kotlin,role="secondary"]
  59. ----
  60. @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
  61. ----
  62. ====
  63. Additionally, note that `@EnableMethodSecurity` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  64. If after moving to `@EnableMethodSecurity` you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
  65. ==== Publish your custom `PermissionEvaluator` as a `MethodSecurityExpressionHandler`
  66. `@EnableMethodSecurity` does not pick up a `PermissionEvaluator` bean.
  67. Instead, it picks up the more generic `MethodSecurityExpressionHandler` to simplify the API.
  68. If you have a custom {security-api-url}org/springframework/security/access/PermissionEvaluator.html[`PermissionEvaluator`] `@Bean`, please change it from:
  69. ====
  70. .Java
  71. [source,java,role="primary"]
  72. ----
  73. @Bean
  74. PermissionEvaluator permissionEvaluator() {
  75. // ... your evaluator
  76. }
  77. ----
  78. .Kotlin
  79. [source,kotlin,role="secondary"]
  80. ----
  81. @Bean
  82. fun permissionEvaluator(): PermissionEvaluator {
  83. // ... your evaluator
  84. }
  85. ----
  86. ====
  87. to:
  88. ====
  89. .Java
  90. [source,java,role="primary"]
  91. ----
  92. @Bean
  93. MethodSecurityExpressionHandler expressionHandler() {
  94. var expressionHandler = new DefaultMethodSecurityExpressionHandler();
  95. expressionHandler.setPermissionEvaluator(myPermissionEvaluator);
  96. return expressionHandler;
  97. }
  98. ----
  99. .Kotlin
  100. [source,kotlin,role="secondary"]
  101. ----
  102. @Bean
  103. fun expressionHandler(): MethodSecurityExpressionHandler {
  104. val expressionHandler = DefaultMethodSecurityExpressionHandler
  105. expressionHandler.setPermissionEvaluator(myPermissionEvaluator)
  106. return expressionHandler
  107. }
  108. ----
  109. ====
  110. == Reactive
  111. === Activate `AuthorizationManager` in `@EnableReactiveMethodSecurity`
  112. 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.
  113. 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.
  114. To opt in, change `useAuthorizationManager` to `true` like so:
  115. ====
  116. .Java
  117. [source,java,role="primary"]
  118. ----
  119. @EnableReactiveMethodSecurity
  120. ----
  121. .Kotlin
  122. [source,kotlin,role="secondary"]
  123. ----
  124. @EnableReactiveMethodSecurity
  125. ----
  126. ====
  127. changes to:
  128. ====
  129. .Java
  130. [source,java,role="primary"]
  131. ----
  132. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  133. ----
  134. .Kotlin
  135. [source,kotlin,role="secondary"]
  136. ----
  137. @EnableReactiveMethodSecurity(useAuthorizationManager = true)
  138. ----
  139. ====
  140. Note that in 6.0, `useAuthorizationManager` defaults to `true`.
  141. Additionally, note that `useAuthorizationManager` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
  142. 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.