javaspring-boot-testspring-ldapldif

How to add the Default data from Ldif file to the Ldap Server with the java api?


I need to add bulk data into the LDAP server from the ldif file. I researched of java APIs but can't find the suitable one

I already tried with LdapTestUtils but it requires a server restart. I need another way except this


Solution

  • It can also be achieved via LdapTemplate. LdapParser will parse the record from ldif file in the form of LdapAttribute then bind this record via ldapTemplate.bind

    LdapContextSource contextSource = new LdapContextSource();
    
            contextSource.setUrl("ldap://192.168.99.101:389/");
            contextSource.setUserDn("uid=admin,dc=abc,dc=com");
            contextSource.setPassword(********);
            contextSource.setPooled(false);
            contextSource.afterPropertiesSet();
    
            LdapTemplate template = new LdapTemplate(contextSource);
            LdifParser parser = new LdifParser(new ClassPathResource("schema.ldif"));
            parser.open();
            while (parser.hasMoreRecords()) {
                LdapAttributes record = parser.getRecord();
                LdapName dn = record.getName();
                template.bind(dn, null, record);
            }