DefaultLdapAuthenticationProvider extends LdapAuthenticationProvider { @Override public Authentication authenticate( Authentication authentication ) throws AuthenticationException { final Authentication authenticate = super.authenticate( authentication ); for ( GrantedAuthority authority : authenticate.getAuthorities() ) { if ( authority.getAuthority().equals( authenticatedRole ) ) { return authenticate; } } throw new AuthenticationServiceException( "The user doesn't have '" + authenticatedRole + "' role." ); } DefaultLdapAuthenticationProvider( LdapAuthenticator authenticator, IAuthenticationRoleMapper roleMapper );  DefaultLdapAuthenticationProvider( LdapAuthenticator authenticator,
      LdapAuthoritiesPopulator authoritiesPopulator, IAuthenticationRoleMapper roleMapper );  DefaultLdapAuthenticationProvider( LdapAuthenticator authenticator,
      LdapAuthoritiesPopulator authoritiesPopulator, IAuthenticationRoleMapper roleMapper, String authenticatedRole ); @Override Authentication authenticate( Authentication authentication );  }
@Test( expected = IllegalArgumentException.class ) public void testAuthenticate_badArgs() throws Exception { ldapAuthProvider = new DefaultLdapAuthenticationProvider( authenticator, roleMapper ); ldapAuthProvider.authenticate( auth ); }
@Test public void testAuthenticate() throws Exception { ldapAuthProvider = new DefaultLdapAuthenticationProvider( authenticator, authoritiesPopulator, roleMapper, "admin" ); when( usernamePasswordAuthenticationToken.getName() ).thenReturn( "admin" ); when( usernamePasswordAuthenticationToken.getCredentials() ).thenReturn( "p@$$w0rd" ); DirContextOperations dirContextOps = mock( DirContextOperations.class ); when( authenticator.authenticate( usernamePasswordAuthenticationToken ) ).thenReturn( dirContextOps ); Collection grantedAuthorities = Arrays.asList( new GrantedAuthority[]{ new SimpleGrantedAuthority( "admin" ) } ); when( authoritiesPopulator.getGrantedAuthorities( dirContextOps, "admin" ) ).thenReturn( grantedAuthorities ); UserDetailsContextMapper contextMapper = mock( UserDetailsContextMapper.class ); ldapAuthProvider.setUserDetailsContextMapper( contextMapper ); UserDetails userDetails = mock( UserDetails.class ); when( userDetails.getAuthorities() ).thenReturn( grantedAuthorities ); when( contextMapper.mapUserFromContext( any( DirContextOperations.class ), anyString(), any( grantedAuthorities.getClass() ) ) ).thenReturn( userDetails ); when( roleMapper.toPentahoRole( anyString() ) ).thenReturn( "admin" ); Authentication result = ldapAuthProvider.authenticate( usernamePasswordAuthenticationToken ); assertNotNull( result ); assertEquals( "p@$$w0rd", result.getCredentials().toString() ); }