0
votes

I am trying to connect to LDAP server using spring
The available information about LDAP server is the following:

  1. host/ip
  2. port
  3. domain => ou=x,dc=y,dc=z
    I don't have any info about the filtration method such as uid = {0} or cn = {0} which matches the username

Here is my code

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.ldap.core.support.LdapContextSource;

@Configuration
public class LdapConfiguration {
    
  @Bean
  public LdapContextSource contextSource() {
      LdapContextSource contextSource = new LdapContextSource();
      contextSource.setUrl("ldap://ip:port");
      contextSource.setBase("dc=y,dc=z");
      return contextSource;
  }
    
    @Bean
    public LdapTemplate ldapTemplate() {
        LdapTemplate template = new LdapTemplate(contextSource());
    
        return template;
    }
}

Then in another class here is the authentication method

@Service
public class LdapUserServiceImpl implements LdapUserService, BaseLdapNameAware {

  @Autowired
  protected LdapTemplate ldapTemplate;
  
  @Autowired
  protected ContextSource contextSource;

  @Override
  public Boolean authenticate(String userDn, String credentials) {

      AndFilter filter = new AndFilter();
      filter.and(new EqualsFilter("uid", userDn));
      boolean authenticated = ldapTemplate.authenticate(LdapUtils.emptyLdapName(), filter.toString(), credentials);
      
      return authenticated;
   }
}
      

I have got the following error:

m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.ldap.UncategorizedLdapException: Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C090A7D, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v3839

My question is what is the reason for this error, also what can I do if no pattern is known as uid={0} or it is standard
Also, I tried to put in the ContextSource initialization username and password, although they are assumed not to be available by me

  contextSource.setUserDn("uid=username,ou=x,dc=y,dc=z"); 
  contextSource.setPassword("password");

This gives me the following error:

[nio-8005-exec-5] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090453, comment: AcceptSecurityContext error, data 52e, v3839

In the application.properties file, I put

spring.ldap.embedded.base-dn=dc=y,dc=z
spring.ldap.embedded.port=port
1

1 Answers

0
votes

Set userDn as [email protected]. contextSource.setUserDn("uid=username,ou=x,dc=y,dc=z"); Here to me, ou=x, looks to be extra, below statement should make it work. contextSource.setUserDn("uid=username,dc=y,dc=z");

Regards Abhi