title: How to add entry to LDIF file using Spring Boot embedded ldap server
body: <p>I have built a spring boot REST app with LDAP authentication using unboundid as embedded ldap server. Authentication is based on simple LDIF file, and now I need the ability to add new entries to this file, so I could to authenticate with ones later. How can I save new entry directly to the LDIF?</p>
  
  <p>I have tried to do that using <code>LdapTemplate</code>, but it works only for one session of application(as I understood, <code>LdapTemplate</code> adds new entry to some "internal, one-session-living" LDAP) and when application stops, LDIF file remains unchanged.</p>
  
  <p>Here is my <strong>application.properties</strong> file</p>
  
  <pre><code>#LDAP config
  spring.ldap.embedded.base-dn=dc=time-tracking-service,dc=com
  spring.ldap.embedded.credential.username=uid=admin
  spring.ldap.embedded.credential.password=pass1
  spring.ldap.embedded.ldif=classpath:users.ldif
  spring.ldap.embedded.validation.enabled=false
  spring.ldap.embedded.port=8389
  ldap.url=ldap://localhost:8389/
  </code></pre>
  
  <p>This is my <strong>entry class</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Entry(
      objectClasses = {"inetOrgPerson", "organizationalPerson", "person", "top"}
  )
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public final class LdapPerson{
  
      @Id
      private Name dn;
  
      @DnAttribute(value = "uid", index = 1)
      private String uid;
  
      @DnAttribute(value = "ou", index = 0)
      @Transient
      private String group;
  
      @Attribute(name = "cn")
      private String fullName;
  
      @Attribute(name = "sn")
      private String lastName;
  
      @Attribute(name = "userPassword")
      private String password;
  
      public LdapPerson(String uid, String fullName, String lastName, String group, String password) {
          this.dn = LdapNameBuilder.newInstance("uid=" + uid + ",ou=" + group).build();
          this.uid = uid;
          this.fullName = fullName;
          this.lastName = lastName;
          this.group = group;
          this.password = password;
      }
  </code></pre>
  
  <p>And my <strong>LdapConfig</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @PropertySource("classpath:application.properties")
  @EnableLdapRepositories
  public class LdapConfig {
  
      @Autowired
      private Environment env;
  
      @Bean
      public LdapContextSource contextSource() {
          LdapContextSource contextSource = new LdapContextSource();
          contextSource.setUrl(env.getProperty("ldap.url"));
          contextSource.setBase(env.getRequiredProperty("spring.ldap.embedded.base-dn"));
          contextSource.setUserDn(env.getRequiredProperty("spring.ldap.embedded.credential.username"));
          contextSource.setPassword(env.getRequiredProperty("spring.ldap.embedded.credential.password"));
          contextSource.afterPropertiesSet();
          return contextSource;
      }
  
      @Bean
      public LdapTemplate ldapTemplate() {
          return new LdapTemplate(contextSource());
      }
  }
  </code></pre>
  
  <p>I add entry simply using</p>
  
  <pre class="lang-java prettyprint-override"><code>ldapTemplate.create(ldapPerson);
  </code></pre>
  
  <p>I expected that using <code>LdapTemplate</code> I will be able to add new entry to the LDIF file, but it doesn't work, so I need help with this problem.</p>