@Provides @Singleton SecurityManager provideSecurityManager( SessionManager sessionManager, RememberMeManager rememberMeManager, Realms realms, Set<AuthenticationListener> authListeners) { DefaultSecurityManager manager = new DefaultSecurityManager(realms.getRealms()); ((AbstractAuthenticator) manager.getAuthenticator()).setAuthenticationListeners(authListeners); manager.setSessionManager(sessionManager); manager.setRememberMeManager(rememberMeManager); return manager; }
@Test public void testFullStack_AuthListener() { Realm mockRealm = mockRealm(); AuthenticationListener mockListener = mock(AuthenticationListener.class); BQRuntime runtime = testFactory.app() .module(b -> ShiroModule .extend(b) .addRealm(mockRealm) .addAuthListener(mockListener)) .autoLoadModules() .createRuntime(); Subject subject = new Subject.Builder(runtime.getInstance(SecurityManager.class)).buildSubject(); assertFalse(subject.isAuthenticated()); // try bad login try { subject.login(new UsernamePasswordToken("uname", "badpassword")); Assert.fail("Should have thrown on bad auth"); } catch (AuthenticationException authEx) { verify(mockListener).onFailure(any(AuthenticationToken.class), any(AuthenticationException.class)); } // try good login subject.login(new UsernamePasswordToken("uname", "password")); verify(mockListener).onSuccess(any(AuthenticationToken.class), any(AuthenticationInfo.class)); }
@Singleton @Provides WebSecurityManager provideWebSecurityManager( SessionManager sessionManager, RememberMeManager rememberMeManager, Realms realms, Set<AuthenticationListener> authListeners) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(realms.getRealms()); // TODO: from here the code is copied from ShiroModule ... error prone... use factory or something ((AbstractAuthenticator) securityManager.getAuthenticator()).setAuthenticationListeners(authListeners); securityManager.setSessionManager(sessionManager); securityManager.setRememberMeManager(rememberMeManager); return securityManager; }
protected Multibinder<AuthenticationListener> contributeListeners() { return listeners != null ? listeners : (listeners = newSet(AuthenticationListener.class)); }
/** * @param listener a listener instance. * @return this extender instance. * @since 0.25 */ public ShiroModuleExtender addAuthListener(AuthenticationListener listener) { contributeListeners().addBinding().toInstance(listener); return this; }
/** * @param listenerType a class of the auth listener * @return this extender instance. * @since 0.25 */ public ShiroModuleExtender addAuthListener(Class<? extends AuthenticationListener> listenerType) { contributeListeners().addBinding().to(listenerType); return this; }