events.adoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. [[servlet-events]]
  2. = Authorization Events
  3. For each authorization that is denied, an `AuthorizationDeniedEvent` is fired.
  4. Also, it's possible to fire an `AuthorizationGrantedEvent` for authorizations that are granted.
  5. To listen for these events, you must first publish an `AuthorizationEventPublisher`.
  6. Spring Security's `SpringAuthorizationEventPublisher` will probably do fine.
  7. It comes publishes authorization events using Spring's `ApplicationEventPublisher`:
  8. [tabs]
  9. ======
  10. Java::
  11. +
  12. [source,java,role="primary"]
  13. ----
  14. @Bean
  15. public AuthorizationEventPublisher authorizationEventPublisher
  16. (ApplicationEventPublisher applicationEventPublisher) {
  17. return new SpringAuthorizationEventPublisher(applicationEventPublisher);
  18. }
  19. ----
  20. Kotlin::
  21. +
  22. [source,kotlin,role="secondary"]
  23. ----
  24. @Bean
  25. fun authorizationEventPublisher
  26. (applicationEventPublisher: ApplicationEventPublisher?): AuthorizationEventPublisher {
  27. return SpringAuthorizationEventPublisher(applicationEventPublisher)
  28. }
  29. ----
  30. ======
  31. Then, you can use Spring's `@EventListener` support:
  32. [tabs]
  33. ======
  34. Java::
  35. +
  36. [source,java,role="primary"]
  37. ----
  38. @Component
  39. public class AuthenticationEvents {
  40. @EventListener
  41. public void onFailure(AuthorizationDeniedEvent failure) {
  42. // ...
  43. }
  44. }
  45. ----
  46. Kotlin::
  47. +
  48. [source,kotlin,role="secondary"]
  49. ----
  50. @Component
  51. class AuthenticationEvents {
  52. @EventListener
  53. fun onFailure(failure: AuthorizationDeniedEvent?) {
  54. // ...
  55. }
  56. }
  57. ----
  58. ======
  59. [[authorization-granted-events]]
  60. == Authorization Granted Events
  61. Because ``AuthorizationGrantedEvent``s have the potential to be quite noisy, they are not published by default.
  62. In fact, publishing these events will likely require some business logic on your part to ensure that your application is not inundated with noisy authorization events.
  63. You can provide your own predicate that filters success events.
  64. For example, the following publisher only publishes authorization grants where `ROLE_ADMIN` was required:
  65. [tabs]
  66. ======
  67. Java::
  68. +
  69. [source,java,role="primary"]
  70. ----
  71. @Bean
  72. AuthorizationEventPublisher authorizationEventPublisher() {
  73. SpringAuthorizationEventPublisher eventPublisher = new SpringAuthorizationEventPublisher();
  74. eventPublisher.setShouldPublishEvent((result) -> {
  75. if (!result.isGranted()) {
  76. return true;
  77. }
  78. if (result instanceof AuthorityAuthorizationDecision decision) {
  79. Collection<GrantedAuthority> authorities = decision.getAuthorities();
  80. return AuthorityUtils.authorityListToSet(authorities).contains("ROLE_ADMIN");
  81. }
  82. return false;
  83. });
  84. return eventPublisher;
  85. }
  86. ----
  87. Kotlin::
  88. +
  89. [source,kotlin,role="secondary"]
  90. ----
  91. @Bean
  92. fun authorizationEventPublisher(): AuthorizationEventPublisher {
  93. val eventPublisher = SpringAuthorizationEventPublisher()
  94. eventPublisher.setShouldPublishEvent { (result) ->
  95. if (!result.isGranted()) {
  96. return true
  97. }
  98. if (decision is AuthorityAuthorizationDecision) {
  99. val authorities = decision.getAuthorities()
  100. return AuthorityUtils.authorityListToSet(authorities).contains("ROLE_ADMIN")
  101. }
  102. return false
  103. }
  104. return eventPublisher
  105. }
  106. ----
  107. ======