1
votes

I am trying to do LDAP authentication by Spring Security. But it returns an error:

error code 49 - 80090308: LdapErr: DSID-0C0903D9, comment: AcceptSecurityContext error, data 52e, v2580 ]

My code:

auth.ldapAuthentication()
            .contextSource().url("ldap://server:389/dc=main,dc=domain,dc=ru")
            .managerDn("uid=user,ou=Domain Users,dc=mydomain,dc=ru").managerPassword("password")
            .and()
            .userSearchBase("ou=student")
            .userSearchFilter("(cn={0})");
}

What can be the kind of mistake (excluding wrong credentials)?

1
Not sure if it is the origin of the error, but the search base needs to be a RDN which goes all the way through the root. For example : ou=student,dc=main,dc=domain,dc=ru - Esteban
Microsoft AD. Now changed from uid to CN and connect to server . But now , another problem LDAP: error code 32 , problem 2001 (NO_OBJECT) )) - Roman Danileyko
how to correct compare credentials with AD. I every time get 401 Bad credentials. How can i guess it happens from wrong password encoder. Or i wrong? - Roman Danileyko

1 Answers

0
votes

It's work ... maybe anybody willbe helpfull auth.authenticationProvider(ldapAuthenticationProvider()); auth.eraseCredentials(true);

@Bean
public DefaultSpringSecurityContextSource contextSource(){

DefaultSpringSecurityContextSource contextSource =
        new DefaultSpringSecurityContextSource(Arrays.asList("ldap://url:389/"),"dc=ttu,dc=ru");
contextSource.setUserDn(userDn);
contextSource.setPassword(passwordForLDAP);
contextSource.setReferral("follow");
return contextSource;
}

@Bean
public LdapAuthenticationProvider ldapAuthenticationProvider(){
return new 
LdapAuthenticationProvider(ldapAuthenticator(),ldapAuthoritiesPopulator());
}

@Bean
public LdapAuthenticator ldapAuthenticator(){
BindAuthenticator authenticator = new BindAuthenticator(contextSource());
authenticator.setUserSearch(userSearch());
return authenticator;
}

@Bean
public DefaultLdapAuthoritiesPopulator ldapAuthoritiesPopulator(){
 DefaultLdapAuthoritiesPopulator ldapAuthoritiesPopulator =
        new DefaultLdapAuthoritiesPopulator(contextSource(),"ou=TTU");
ldapAuthoritiesPopulator.setSearchSubtree(true);
ldapAuthoritiesPopulator.setIgnorePartialResultException(true);
//ldapAuthoritiesPopulator.setGroupSearchFilter("member={0}");
ldapAuthoritiesPopulator.setRolePrefix("ROLE_");
ldapAuthoritiesPopulator.setConvertToUpperCase(true);
return ldapAuthoritiesPopulator;
}

@Bean
public FilterBasedLdapUserSearch userSearch(){
FilterBasedLdapUserSearch filterBasedLdapUserSearch =
        new FilterBasedLdapUserSearch("","(sAMAccountName={0})",contextSource());
filterBasedLdapUserSearch.setSearchSubtree(true);
return filterBasedLdapUserSearch;
}