ChainedAuthenticationManager implements AuthenticationManager { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (authentication == null) { return authentication; } UsernamePasswordAuthenticationToken output = null; if (authentication instanceof UsernamePasswordAuthenticationToken) { output = (UsernamePasswordAuthenticationToken) authentication; } else { output = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), authentication.getAuthorities()); output.setDetails(authentication.getDetails()); } boolean authenticated = false; Authentication auth = null; AuthenticationException lastException = null; boolean lastResult = false; boolean shallContinue = true; if (delegates==null || delegates.length==0) { throw new ProviderNotFoundException("No available authentication providers."); } for (int i=0; shallContinue && i<delegates.length; i++) { boolean shallAuthenticate = (i==0) || (lastResult && IF_PREVIOUS_TRUE.equals(delegates[i].getRequired())) || ((!lastResult) && IF_PREVIOUS_FALSE.equals(delegates[i].getRequired())); if (shallAuthenticate) { if (logger.isDebugEnabled()) { logger.debug("Attempting chained authentication of " + output + " with manager:" + delegates[i].getAuthenticationManager() + " required:" + delegates[i].getRequired()); } Authentication thisAuth = null; try { thisAuth = delegates[i].getAuthenticationManager().authenticate(auth!=null ? auth : output); } catch (AuthenticationException x) { if (logger.isDebugEnabled()) { logger.debug("Chained authentication exception:"+x.getMessage()+" at:"+(x.getStackTrace().length>0?x.getStackTrace()[0]:"(no stack trace)")); } lastException = x; if (delegates[i].getStopIf()!=null) { for (Class<? extends AuthenticationException> exceptionClass : delegates[i].getStopIf()) { if (exceptionClass.isAssignableFrom(x.getClass())) { shallContinue = false; break; } } } } lastResult = thisAuth != null && thisAuth.isAuthenticated(); if (lastResult) { authenticated = true; auth = thisAuth; } else { authenticated = false; auth = null; } } else { shallContinue = false; } if (logger.isDebugEnabled()) { logger.debug("Chained Authentication status of "+output+ " with manager:"+delegates[i]+"; Authenticated:"+authenticated); } } if (authenticated) { return auth; } else if (lastException!=null) { throw lastException; } else { return auth; } } ChainedAuthenticationManager(); AuthenticationManagerConfiguration[] getDelegates(); void setDelegates(AuthenticationManagerConfiguration[] delegates); @Override Authentication authenticate(Authentication authentication); static final String IF_PREVIOUS_FALSE; static final String IF_PREVIOUS_TRUE; }
@Test public void testUaaAuthTrue() { managers[0].setAuthenticationManager(authenticateTrue); managers[1].setAuthenticationManager(authenticateFalse); Authentication result = authMgr.authenticate(failure); assertNotNull(result); assertTrue(result.isAuthenticated()); verify(authenticateTrue, times(1)).authenticate(any(Authentication.class)); verify(authenticateFalse, times(0)).authenticate(any(Authentication.class)); verify(loginAuthenticationManager, times(0)).authenticate(any(Authentication.class)); }
@Test public void testUaaAuthFalseLdapTrue() { managers[0].setAuthenticationManager(authenticateFalse); managers[1].setAuthenticationManager(authenticateTrue); Authentication result = authMgr.authenticate(failure); assertNotNull(result); assertTrue(result.isAuthenticated()); verify(authenticateTrue, times(1)).authenticate(any(Authentication.class)); verify(authenticateFalse, times(1)).authenticate(any(Authentication.class)); verify(loginAuthenticationManager, times(1)).authenticate(any(Authentication.class)); }
@Test public void testUaaAuthFalseLdapFalse() { managers[0].setAuthenticationManager(authenticateFalse); managers[1].setAuthenticationManager(authenticateFalse); Authentication result = authMgr.authenticate(failure); assertNull(result); verify(authenticateFalse, times(2)).authenticate(any(Authentication.class)); verify(loginAuthenticationManager, times(0)).authenticate(any(Authentication.class)); }
@Test public void testUaaAuthThrowLdapAuthFalse() { managers[0].setAuthenticationManager(authenticateThrow); managers[1].setAuthenticationManager(authenticateFalse); try { authMgr.authenticate(failure); fail("Should have thrown exception"); } catch (BadCredentialsException ignored) { } verify(authenticateThrow, times(1)).authenticate(any(Authentication.class)); verify(authenticateFalse, times(1)).authenticate(any(Authentication.class)); verify(loginAuthenticationManager, times(0)).authenticate(any(Authentication.class)); }
@Test public void testUaaAuthThrowLdapAuthTrue() { managers[0].setAuthenticationManager(authenticateThrow); managers[1].setAuthenticationManager(authenticateTrue); Authentication result = authMgr.authenticate(failure); assertNotNull(result); assertTrue(result.isAuthenticated()); verify(authenticateThrow, times(1)).authenticate(any(Authentication.class)); verify(authenticateTrue, times(1)).authenticate(any(Authentication.class)); verify(loginAuthenticationManager, times(1)).authenticate(any(Authentication.class)); }