1
votes

I trying to integrate ldap with spring using

<dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-ldap</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>

I am trying to fetch all users i have in our test Ldap Server, but when i use findAll method of LdapRepository interface all i get is an empty list. I have read a lot in Internet , a lot of docs but i have not found what is going wrong with this task i am trying to do.

Here is some code samples: Config file:

@Configuration
@PropertySource("classpath:application.yaml")
@EnableLdapRepositories(basePackages = "gr.mpass.aia.ldap")
public class AppConfig {

    @Autowired
    private Environment env;

    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(env.getRequiredProperty("ldap.url"));
        contextSource.setUserDn(env.getRequiredProperty("ldap.principal"));
        contextSource.setPassword(env.getRequiredProperty("ldap.password"));
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(contextSource());
    }

    @Bean
    public LdapClientService ldapClient() {
        return new LdapClientService();
    }

}

Entry Class:

@Entry(base = "cn=eshop,ou=mpass,cn=admin,dc=userlogin,dc=mpass,dc=ltd", objectClasses = { "posixGroup", "inetOrgPerson", "top" })
public class LdapUser {

    @Id
    private Name id;

    private @Attribute(name = "cn") String fullName;
    private @Attribute(name = "mail") String email;
    private @Attribute(name = "uidNumber") String uidNumber;

    public LdapUser() {
    }

    public LdapUser(String fullName, String email, String uIdNumber) {
        this.fullName = fullName;
        this.email = email;
        this.uidNumber = uidNumber;
    }

    public Name getId() {
        return id;
    }

    public void setId(Name id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String password) {
        this.email = email;
    }

    public String getUidNumber() {
        return uidNumber;
    }

    public void setUidNumber(String uidNumber) {
        this.uidNumber = uidNumber;
    }

    @Override
    public String toString() {
        return fullName;
    }

}

Repository:

@Repository
public interface LdapUserRepository extends LdapRepository<LdapUser> {

    LdapUser findByFullName(String fullName);

    LdapUser findByEmail(String email);

    LdapUser findByUidNumber(String uidNumber);

    List<LdapUser> findByEmailLikeIgnoreCase(String email);

    List<LdapUser> findByEmailNotLike(String email);

}

Fetch users query:

  LdapQuery query = query().base("cn=eshop,ou=mpass,cn=admin,dc=userlogin,dc=mpass,dc=ltd")
                .searchScope(SearchScope.ONELEVEL)
                .where("objectclass").is("inetOrgPerson");
        Iterable<LdapUser> ldapUsers = ldapUserRepository.findAll(query);

This returns an empty list.

I tried ldapUserRepository.findAll() without a parameter but it doesn't work. It says javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object].

I tried this answer https://stackoverflow.com/a/48384297/1501205 but it doesnt help at all.

Any help would be appreciated. Thanks in advance Here a printscreen of the Ldap server tree: enter image description here

2

2 Answers

0
votes

You provided full dn of your entry as base in both query and Entry object. I think there is something wrong with these. The right way is:

in Configuration: "cn=admin,dc=userlogin,dc=mpass,dc=ltd" and in Entry: "cn=eshop,ou=mpass"

0
votes

I had a wrong ObjectClass in my Entry . PosixGroup was the wrong . I replaced it with PosixAccount and it works.