2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[servlet-events]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								= Authorization Events
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For each authorization that is denied, an `AuthorizationDeniedEvent` is fired.
							 
						 
					
						
							
								
									
										
										
										
											2023-12-14 17:41:17 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Also, it's possible to fire an `AuthorizationGrantedEvent` for authorizations that are granted.
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To listen for these events, you must first publish an `AuthorizationEventPublisher`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Spring Security's `SpringAuthorizationEventPublisher` will probably do fine.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It comes publishes authorization events using Spring's `ApplicationEventPublisher`:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public AuthorizationEventPublisher authorizationEventPublisher
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        (ApplicationEventPublisher applicationEventPublisher) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return new SpringAuthorizationEventPublisher(applicationEventPublisher);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun authorizationEventPublisher
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        (applicationEventPublisher: ApplicationEventPublisher?): AuthorizationEventPublisher {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return SpringAuthorizationEventPublisher(applicationEventPublisher)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Then, you can use Spring's `@EventListener` support:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Component
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class AuthenticationEvents {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @EventListener
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    public void onFailure(AuthorizationDeniedEvent failure) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Component
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class AuthenticationEvents {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @EventListener
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    fun onFailure(failure: AuthorizationDeniedEvent?) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[authorization-granted-events]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								== Authorization Granted Events
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Because ``AuthorizationGrantedEvent``s have the potential to be quite noisy, they are not published by default.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can provide your own predicate that filters success events.
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								For example, the following publisher only publishes authorization grants where `ROLE_ADMIN` was required:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								AuthorizationEventPublisher authorizationEventPublisher() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    SpringAuthorizationEventPublisher eventPublisher = new SpringAuthorizationEventPublisher();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    eventPublisher.setShouldPublishEvent((result) -> {
							 
						 
					
						
							
								
									
										
										
										
											2025-06-21 01:12:07 +07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        if (!result.isGranted()) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            return true;
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        if (result instanceof AuthorityAuthorizationDecision decision) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            Collection<GrantedAuthority> authorities = decision.getAuthorities();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return AuthorityUtils.authorityListToSet(authorities).contains("ROLE_ADMIN");
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return false;
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return eventPublisher;
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun authorizationEventPublisher(): AuthorizationEventPublisher {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val eventPublisher = SpringAuthorizationEventPublisher()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    eventPublisher.setShouldPublishEvent { (result) ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (!result.isGranted()) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return true
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        if (decision is AuthorityAuthorizationDecision) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            val authorities = decision.getAuthorities()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return AuthorityUtils.authorityListToSet(authorities).contains("ROLE_ADMIN")
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return false
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2025-07-07 17:54:34 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return eventPublisher
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 15:22:38 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======