authorization.adoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. = Authorization Changes
  2. The following sections relate to how to adapt to changes in the authorization support.
  3. == Method Security
  4. [[compile-with-parameters]]
  5. === Compile With `-parameters`
  6. Spring Framework 6.1 https://github.com/spring-projects/spring-framework/issues/29559[removes LocalVariableTableParameterNameDiscoverer].
  7. This affects how `@PreAuthorize` and other xref:servlet/authorization/method-security.adoc[method security] annotations will process parameter names.
  8. If you are using method security annotations with parameter names, for example:
  9. [source,java]
  10. .Method security annotation using `id` parameter name
  11. ----
  12. @PreAuthorize("@authz.checkPermission(#id, authentication)")
  13. public void doSomething(Long id) {
  14. // ...
  15. }
  16. ----
  17. You must compile with `-parameters` to ensure that the parameter names are available at runtime.
  18. For more information about this, please visit the https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#core-container[Upgrading to Spring Framework 6.1 page].
  19. === Favor `AnnotationTemplateExpressionDefaults` over `PrePostTemplateDefaults`
  20. In Spring Security 7, `AnnotationTemplateExpressionDefaults` will be included by default.
  21. If you are customizing `PrePostTemplateDefaults` or simply want to see how your application responds to `AnnotationTemplateExpressionDefaults`, you can publish an `AnnotationTemplateExpressionDefaults` bean instead of a `PrePostTemplateDefaults` method:
  22. [tabs]
  23. ======
  24. Java::
  25. +
  26. [source,java,role="primary"]
  27. ----
  28. @Bean
  29. static AnnotationTemplateExpressionDefaults templateExpressionDefaults() {
  30. return new AnnotationTemplateExpressionDefaults();
  31. }
  32. ----
  33. Kotlin::
  34. +
  35. [source,kotlin,role="secondary"]
  36. ----
  37. companion object {
  38. @Bean
  39. fun templateExpressionDefaults() = AnnotationTemplateExpressionDefaults()
  40. }
  41. ----
  42. Xml::
  43. +
  44. [source,xml,role="secondary"]
  45. ----
  46. <b:bean id="templateExpressionDefaults" class="org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults"/>
  47. ----
  48. ======
  49. ==== I Am Publishing an AuthorizationAdvisor Bean
  50. If you are publishing an `AuthorizationAdvisor` bean, like `AuthorizationManagerBeforeMethodInterceptor`, `AuthorizationManagerAfterMethodInterceptor`, `PreFilterAuthorizationMethodInterceptor`, or `PostFilterAuthorizationMethodInterceptor`, you can do the same by calling `setTemplateDefaults` with an `AnnotationTemplateExpressionDefaults` instance instead:
  51. [tabs]
  52. ======
  53. Java::
  54. +
  55. [source,java,role="primary"]
  56. ----
  57. @Bean
  58. @Role(BeanDescription.ROLE_INFRASTRUCTURE)
  59. static Advisor preFilter() {
  60. PreFilterAuthorizationMethodInterceptor interceptor = new PreFilterAuthorizationMethodInterceptor();
  61. interceptor.setTemplateDefaults(new AnnotationTemplateExpressionDefaults());
  62. return interceptor;
  63. }
  64. ----
  65. Kotlin::
  66. +
  67. [source,kotlin,role="secondary"]
  68. ----
  69. companion object {
  70. @Bean
  71. @Role(BeanDescription.ROLE_INFRASTRUCTURE)
  72. fun preFilter(): Advisor {
  73. val interceptor = PreFilterAuthorizationMethodInterceptor()
  74. interceptor.setTemplateDefaults(AnnotationTemplateExpressionDefaults)
  75. return interceptor
  76. }
  77. }
  78. ----
  79. ======
  80. === Publish `AuthorizationAdvisor` instances instead of adding them in a `Customizer<AuthorizationAdvisorProxyFactory>`
  81. While the ability to customize the `AuthorizationAdvisorProxyFactory` instance will remain in Spring Security 7, the ability to add advisors will be removed in favor of picking up published `AuthorizationAdvisor` beans.
  82. If you are not calling `AuthorizationAdvisorProxyFactory#setAdvisors` or `AuthorizationAdvisorProxyFactory#addAdvisor`, you need do nothing.
  83. If you are, publish the `AuthorizationAdvisor` bean instead and Spring Security will pick it up and apply it automatically.