Giriş
import org.springframework.ldap.core.LdapTemplate;
Maven
<dependency>
  <groupId>org.springframework.ldap</groupId>
  <artifactId>spring-ldap-core</artifactId>
</dependency>
Tanımlama
Şöyle 
yaparız.
<ldap:context-source
    id="ldapContextSource"
    url="ldap://ldaphost:389"
    username="cn=ldaptestadmin,cn=Administrators,cn=config"
    password="abcxyz"
    base="dc=studentBase,dc=example,dc=com"/>
<ldap:ldap-template id="ldapTemplate" context-source-
    ref="ldapContextSource"/>
create metodu
Örnek ver
 
authenticate metodu
Örnek
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.Filter;
import org.springframework.ldap.support.LdapUtils;
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://localhost:8389/dc=springframework,dc=org");
contextSource.setAnonymousReadOnly(true);
contextSource.setUserDn("uid={0},ou=people");
contextSource.afterPropertiesSet();
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
Filter filter = new EqualsFilter("uid", "myname");
Boolean authenticated = ldapTemplate.authenticate(LdapUtils.emptyLdapName(),
  filter.encode(), "mypassword");
if (authenticated) {
  ...
}
DefaultIncrementalAttributesMapper İle Kullanımı
If you know the distinguished name (dn), you can use the DefaultIncrementAttributesMapper to look up for multi-valued attributes (eg. the mail attribute).
Örnek
@Service
class LdapQueryService(
    private val ldapTemplate: LdapTemplate
) {
  private fun getMultiValuedAttributesWithDefaultIncrementAttributesMapper(dn: String,
      attr: String): List<String> {
    val attributes = DefaultIncrementalAttributesMapper.lookupAttributes(ldapTemplate,
      dn, attr)
    val results = ArrayList<String>()
    for (i in 0 until attributes.get(attr).size()) {
      results.add(attributes.get(attr)[i].toString())
    }
    return results
  }
    
  fun query(): List<String> {
    val dn = "cn=Amanda,ou=Users"
    val attr = "mail"
    val emails = getMultiValuedAttributesWithDefaultIncrementAttributesMapper(dn, attr)
    return emails
  }
}
search metodu - AttributesMapper
Internally, LdapTemplate iterates over all entries found, calling the given AttributesMapper for each entry, and collects the results in a list. The list is then returned by the search method.
  
  Örnek
@Service
class LdapQueryService(
  private val ldapTemplate: LdapTemplate
) {
  fun queryWithAttributeMapper(userId: String): List<User> {
    return ldapTemplate.search("ou=Users", "uid=$userId", AttributesMapper { attributes ->
      val id = attributes.get("uid").get().toString()
      val name = attributes.get("displayname").get().toString()
      val emails = attributes.get("mail").all.toList() as List<String>
      User(userId = id, name = name, emails = emails)
    })
  }
}
search metodu - ContextMapper
Whenever an entry is found in the LDAP tree, its attributes and Distinguished Name (DN) will be used by Spring LDAP to construct a DirContextAdapter. This enables us to use a ContextMapper to transform the found values.
    
    The advantage of the ContextMapper is that
- it handles NullPointerException by returning null instead of throwing exceptions.
- it simplifies attributes retrieval operations (especially for multi-value attributes) with getStringAttribute() and getStringAttributes() methods.
- it allows you to retrieve distinguished name (dn), schemas and object classes.
  
   Örnek
@Service
class LdapQueryService(
    private val ldapTemplate: LdapTemplate
) {
  fun queryWithContextMapper(userId: String): List<User> {
    return ldapTemplate.search("ou=Users", "uid=$userId",
      object: AbstractContextMapper<User>() {
      override fun doMapFromContext(ctx: DirContextOperations): AdUser {
        val dn = ctx.dn
        val id = ctx.getStringAttribute("uid")
        val name = ctx.getStringAttribute("displayname")
        val emails = ctx.getStringAttributes("mail").toList()
        return User(id, name, emails)
      }
    })
  }
}