10 Haziran 2018 Pazar

LDAP InitialLdapContext Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.naming.ldap.InitialLdapContext;
LDAP bağlantısı için InitialDirContext Sınıfı da kullanılabilir.

constructor
Örnek
Şöyle yaparız.
env.put(Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL, "ldap://localhost");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=XXXXXXX");
env.put(Context.SECURITY_CREDENTIALS, "YYYYYYY");

LdapContext ctx = new InitialLdapContext(env, null);
Örnek
Şöyle yaparız.
Hashtable<String, String> ldapContextDetails = new Hashtable<>();
ldapContextDetails.put(Context.INITIAL_CONTEXT_FACTORY,
  "com.sun.jndi.ldap.LdapCtxFactory");
ldapContextDetails.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapContextDetails.put(Context.SECURITY_PRINCIPAL, "username");
ldapContextDetails.put(Context.SECURITY_CREDENTIALS, "password");
ldapContextDetails.put(Context.REFERRAL, "follow");
ldapContextDetails.put("com.sun.jndi.ldap.connect.timeout", "10000");
ldapContextDetails.put(Context.SECURITY_PROTOCOL, "ssl");

String providerUrl =  "ldap://domain1.com:636/";
ldapContextDetails.put(Context.PROVIDER_URL, providerUrl);
InitialLdapContext ldapContext;
try {
  ldapContext = new InitialLdapContext(ldapContextDetails, null);
  System.out.println("connected");
} catch (NamingException e) {
  e.printStackTrace();
}
close metodu
Şöyle yaparız.
ctx.close();
search metodu
Şöyle yaparız.
SearchControls  sc = new SearchControls(SearchControls.SUBTREE_SCOPE, 0, 0, null, false,
  false);
for (;;)
{
  /* Perform search */
  NamingEnumeration<SearchResult> ne =
    ctx.search("ou=Users,dc=xxxx,dc=com", "(objectClass={0})",
      new String[]{"inetOrgPerson"}, sc);

  /* Enumerate search results */
  while (ne.hasMore())
  {
     SearchResult sr = ne.next();
     System.out.println(count+": "+sr.getNameInNamespace());
   }

  ne.close();
  ...
}
setRequestControls metodu
Şöyle yaparız.
/* Sort Control is required for VLV to work */
SortKey[] sortKeys =
{
  // sort by cn 
  new SortKey("cn", true, "caseIgnoreOrderingMatch")
};
// Note: the constructors for SortControl that take String or String[]
// as the first argument produce 'no ordering  rule' errors with OpenLDAP.
SortControl sctl = new SortControl(
  // "cn",
  //  new String[]{"cn"},
  sortKeys,
  Control.CRITICAL);

/* VLV that returns the first 20 answers */
VirtualListViewControl vctl =
  new VirtualListViewControl(1, 0, 0, LIST_SIZE-1, Control.CRITICAL);

/* Set context's request controls */
ctx.setRequestControls(new Control[]
{
  sctl,
  vctl
});


Hiç yorum yok:

Yorum Gönder