events.adoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. [[servlet-events]]
  2. = Authorization Events
  3. For each authorization that is denied, an `AuthorizationDeniedEvent` is fired.
  4. Also, it's possible to fire and `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 create your own event publisher 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. @Component
  72. public class MyAuthorizationEventPublisher implements AuthorizationEventPublisher {
  73. private final ApplicationEventPublisher publisher;
  74. private final AuthorizationEventPublisher delegate;
  75. public MyAuthorizationEventPublisher(ApplicationEventPublisher publisher) {
  76. this.publisher = publisher;
  77. this.delegate = new SpringAuthorizationEventPublisher(publisher);
  78. }
  79. @Override
  80. public <T> void publishAuthorizationEvent(Supplier<Authentication> authentication,
  81. T object, AuthorizationDecision decision) {
  82. if (decision == null) {
  83. return;
  84. }
  85. if (!decision.isGranted()) {
  86. this.delegate.publishAuthorizationEvent(authentication, object, decision);
  87. return;
  88. }
  89. if (shouldThisEventBePublished(decision)) {
  90. AuthorizationGrantedEvent granted = new AuthorizationGrantedEvent(
  91. authentication, object, decision);
  92. this.publisher.publishEvent(granted);
  93. }
  94. }
  95. private boolean shouldThisEventBePublished(AuthorizationDecision decision) {
  96. if (!(decision instanceof AuthorityAuthorizationDecision)) {
  97. return false;
  98. }
  99. Collection<GrantedAuthority> authorities = ((AuthorityAuthorizationDecision) decision).getAuthorities();
  100. for (GrantedAuthority authority : authorities) {
  101. if ("ROLE_ADMIN".equals(authority.getAuthority())) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. }
  108. ----
  109. Kotlin::
  110. +
  111. [source,kotlin,role="secondary"]
  112. ----
  113. @Component
  114. class MyAuthorizationEventPublisher(val publisher: ApplicationEventPublisher,
  115. val delegate: SpringAuthorizationEventPublisher = SpringAuthorizationEventPublisher(publisher)):
  116. AuthorizationEventPublisher {
  117. override fun <T : Any?> publishAuthorizationEvent(
  118. authentication: Supplier<Authentication>?,
  119. `object`: T,
  120. decision: AuthorizationDecision?
  121. ) {
  122. if (decision == null) {
  123. return
  124. }
  125. if (!decision.isGranted) {
  126. this.delegate.publishAuthorizationEvent(authentication, `object`, decision)
  127. return
  128. }
  129. if (shouldThisEventBePublished(decision)) {
  130. val granted = AuthorizationGrantedEvent(authentication, `object`, decision)
  131. this.publisher.publishEvent(granted)
  132. }
  133. }
  134. private fun shouldThisEventBePublished(decision: AuthorizationDecision): Boolean {
  135. if (decision !is AuthorityAuthorizationDecision) {
  136. return false
  137. }
  138. val authorities = decision.authorities
  139. for (authority in authorities) {
  140. if ("ROLE_ADMIN" == authority.authority) {
  141. return true
  142. }
  143. }
  144. return false
  145. }
  146. }
  147. ----
  148. ======