How to register event listeners and subscribers

PlayAuth / Shiro has various events that can be used to trigger custom behavior in your application.

You can add event listeners using EventDispatcher like follows:

Add event listener

PlayAuth.getEventDispatcher().addListener(PartyLoginSuccessEvent.class, new EventListener<PartyLoginSuccessEvent>() {
    public void handle(PartyLoginSuccessEvent event) {"Welcome back, " + event.getParty().getName() + "!");


EventDispatcher.addListener accepts an event type that you want to listen for (PartyLoginSuccessEvent.class in the example above) and actual event handler that accepts event object with contextual information.


You can also group several event listeners into one event subscriber class:

Add event subscriber

public class MyAuthSubscriber implements EventSubscriber {
    public Collection<Class<? extends EventObject>> getSubscribedEvents() {
        return Arrays.asList(PartyLoginSuccessEvent.class, PartyLoginFailureEvent.class, PartyLogoutEvent.class);
    public void handle(EventObject event) {
        if (event instanceof PartyLoginSuccessEvent) {
            on((PartyLoginSuccessEvent) event);
        } else if (event instanceof PartyLoginFailureEvent) {
            on((PartyLoginFailureEvent) event);
        } else if (event instanceof PartyLogoutEvent) {
            on((PartyLogoutEvent) event);
    private void on(PartyLoginSuccessEvent e) {
        Logger.debug("Authenticated user {}", e.getParty().getIdentifier());
    private void on(PartyLoginFailureEvent e) {
        Logger.debug("Failed to authenticate user by submitted identity {}", e.getToken().getPrincipal());
    private void on(PartyLogoutEvent e) {
        Logger.debug("Logged out user {}", e.getParty().getIdentifier());

Then add event subscriber using EventDispatcher:

PlayAuth.getEventDispatcher().addSubscriber(new MyAuthSubscriber());

How to unregister event listeners and subscriber

You can remove event listeners by their type or by  listener object's reference:

// remove all listeners of PartyLoginSuccessEvent
// remove specific instance of event listener
EventListener listener =  new EventListener<PartyLoginSuccessEvent>() { ... }
PlayAuth.getEventDispatcher().addListener(PartyLoginSuccessEvent.class, listener);
// remove event subscriber


Note that addSubscriber is equivalent to calling EventDispatcher.addListener(someType, subscriber) with each of  EventSubscriber.getSubscribedEvents, i.e. removeListeners(PartyLoginSuccessEvent.class) will also remove those from added event subscriber.

Available events

The following events are available at this moment:

  • PartyLoginSuccessEvent
  • PartyLoginFailureEvent
  • PartyLogoutEvent
  • PartyStartImpersonationEvent
  • PartyEndImpersonationEvent
  • PermissionGrantEvent
  • PermissionRevokeEvent