Obtaining a reference to the base LDAP path

As described above, a base LDAP path may be supplied to the ContextSource, specifying the root in the LDAP tree to which all operations will be relative. This means that you will only be working with relative distinguished names throughout your system, which is typically rather handy. There are however some cases in which you will need to have access to the base path in order to be able to construct full DNs, relative to the actual root of the LDAP tree. One example would be when working with LDAP groups (e.g. groupOfNames objectclass), in which case each group member attribute value will need to be the full DN of the referenced member.

For that reason, Spring LDAP has a mechanism by which any Spring controlled bean may be supplied the base path on startup. For beans to be notified of the base path, two things need to be in place: First of all, the bean that wants the base path reference needs to implement the BaseLdapPathAware interface. Secondly, a BaseLdapPathBeanPostProcessor needs to be defined in the application context

Example 8.3. Implementing BaseLdapPathAware

package com.example.service;

public class PersonService implements PersonService, BaseLdapPathAware {
   ...
   private DistinguishedName basePath;
   
   public void setBaseLdapPath(DistinguishedName basePath) {
      this.basePath = basePath;
   }
   ...
   private DistinguishedName getFullPersonDn(Person person) {
      return new DistinguishedName(basePath).append(person.getDn());
   }
   ...
}

Example 8.4. Specifying a BaseLdapPathBeanPostProcessor in your ApplicationContext

<beans>
   ...
   <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
      <property name="url" value="ldap://localhost:389" />
      <property name="base" value="dc=example,dc=com" />
      <property name="authenticationSource" ref="authenticationSource" />
   </bean>
   ...
   <bean class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor" />
</beans>
  	  

The default behaviour of the BaseLdapPathBeanPostProcessor is to use the base path of the single defined BaseLdapPathSource (AbstractContextSource )in the ApplicationContext. If more than one BaseLdapPathSource is defined, you will need to specify which one to use with the baseLdapPathSourceName property.