UnionizingLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator, InitializingBean { public Collection<? extends GrantedAuthority> getGrantedAuthorities( final DirContextOperations userData, final String username ) { Set<GrantedAuthority> allAuthorities = new HashSet<GrantedAuthority>(); for ( LdapAuthoritiesPopulator populator : populators ) { Collection<? extends GrantedAuthority> auths = populator.getGrantedAuthorities( userData, username ); if ( ( null != auths ) && ( auths.size() > 0 ) ) { allAuthorities.addAll( auths ); } } return allAuthorities; } UnionizingLdapAuthoritiesPopulator(); Collection<? extends GrantedAuthority> getGrantedAuthorities( final DirContextOperations userData, final String username ); void setPopulators( final Set<LdapAuthoritiesPopulator> populators ); void afterPropertiesSet();  }
@Test public void testGetGrantedAuthorities() throws Exception { UnionizingLdapAuthoritiesPopulator populator = new UnionizingLdapAuthoritiesPopulator(); Set<LdapAuthoritiesPopulator> pops = new HashSet<>(); pops.add( authPop1 ); pops.add( authPop2 ); String username = "admin"; GrantedAuthority g1 = new SimpleGrantedAuthority( "power user" ); GrantedAuthority g2 = new SimpleGrantedAuthority( "administrator" ); Collection auths1 = Arrays.asList( new GrantedAuthority[]{ g1 } ); Collection auths2 = Arrays.asList( new GrantedAuthority[]{ g2 } ); when( authPop1.getGrantedAuthorities( userData, username ) ).thenReturn( auths1 ); when( authPop2.getGrantedAuthorities( userData, username ) ).thenReturn( auths2 ); populator.setPopulators( pops ); Collection<? extends GrantedAuthority> authorities = populator.getGrantedAuthorities( userData, username ); assertEquals( 2, authorities.size() ); assertTrue( authorities.contains( g1 ) ); assertTrue( authorities.contains( g2 ) ); }