'LdapTemplate return null when programm is running : can't read in Ldap

Good day to you all,

I'm new to Spring Ldap and facing the following issue : I got a null exception error when my program is trying to get all users from my OpenLdap base.

I run off idea why this unexpected beahvior is happening.

ActiveDirectorySettings.java

@Configuration
public class ActiveDirectorySettings {

    @Autowired
        private Environment environment;

    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(environment.getRequiredProperty("spring.ldap.urls"));
        contextSource.setBase(environment.getRequiredProperty("spring.ldap.base"));
        contextSource.setUserDn(environment.getRequiredProperty("spring.ldap.username"));
        contextSource.setPassword(environment.getRequiredProperty("spring.ldap.password"));
        return contextSource;
    }

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

ActiveDirectoryUsersRepository.java

@Service
public class ActiveDirectoryUsersRepository {
@Autowired
private LdapTemplate ldapTemplate;

   public List<String> getAllPersonNames() {
        return ldapTemplate.search(
                query().where("objectclass").is("person"),
                new AttributesMapper<String>() {
                    public String mapFromAttributes(Attributes attrs)
                            throws NamingException {
                        return attrs.get("cn").get().toString();
                    }
                });
    }
}

IntegrationApplication.java

@SpringBootApplication
public class IntegrationApplication {
    public static void main(String[] args) {
        SpringApplication.run(IntegrationApplication.class, args);

        //POC : Getting all AD' users
        ActiveDirectoryUsersRepository activeDirectoryUsersRepository = new ActiveDirectoryUsersRepository();
        List users = activeDirectoryUsersRepository.getAllPersonNames();
        System.out.println(users);
    }
}

Please find a screenshot of my debug run :

enter image description here Many thanks by advance for your help,I wish you a nice day.

Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source