Yes, there is a better way:
- Define a BaseLdapPathBeanPostProcessor in your configuration file.
- Make sure your bean implements BaseLdapPathAware.
Code:
<bean class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor" />
It's quite simple. The post-processor looks for beans implementing BaseLdapPathAware. If it finds any, it injects them with the base path. You can then use that base path in your bean.
Let's say you have a class called MyCleverClass. Make it implement BaseLdapPathAware. You will automatically have the base DN available for you. When you need to remove the base DN from whatever full DNs you are working with, use the DistinguishedName.removeFirst method.
Code:
public class MyCleverClass implements BaseLdapPathAware {
...
private DistinguishedName baseDn;
public void setBaseLdapPath(DistinguishedName baseLdapPath) {
this.baseDn = baseLdapPath;
}
public SomeResult myLookupMethod() {
DistinguishedName dn = new DistinguishedName("uid=userid,ou=People,dc=mycompany,dc=eu");
dn.removeFirst(baseDn);
return ldapTemplate.lookup(dn, myAttributeMapper);
}
}
Define your bean as usual:
Code:
<bean class="com.example.MyCleverClass">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>