Give you an example of an LDAP

Source: Internet
Author: User
Tags ldap mail tostring stringbuffer
/* 1. Extract common name CN from LDAP server, distinguished name uid, password userpassword, email address mail
* Using the Netscape LDAP server as a test environment, log on to the LDAP server using simple authentication.
* 2. Using the name ' admin ' password is ' 1 ', the entire program uses the JNDI standard interface in SDK1.4.1.
* 3. To support Domino database development, assume that the IP address of the user at logon is already recorded in the field UID and separated by ', '
* The program will eventually print a string that includes all user names, passwords, and IP addresses.
* 4. The use of regular expressions is introduced in the process of separating user names and IP addresses.
*/
package mm;

Introduction of LDAP Packages
Import java.lang.*;
Import java.util.Hashtable;
Import java.util.Enumeration;
Import javax.naming.*;
Import javax.naming.directory.*;
Import mm.splitstring;

public class jndisearch{
public static String Initctx = "Com.sun.jndi.ldap.LdapCtxFactory"; Driven
public static String My_host = "ldap://localhost:389"; Host address and Port
public static String my_searchbase = "o=airius.com"; Base Point Entrance
public static String My_filter = "(mail=west)"; Filter conditions
public static String mgr_dn= "Uid=admin,ou=administrators,ou=topologymanagement,o=netscaperoot"; User name
public static String mgr_pw= "1"; Password
public static String my_attrs[] = {/* "cn", "UserPassword", "Mail", */"cn"};
StringBuffer res = new StringBuffer (); Object used to enter the name, IP address
public static String temp = new String ();


Public String Search () throws exception{
StringBuffer res = new StringBuffer ();
try{
Establish a connection
Hashtable env = new Hashtable ();
Env.put (CONTEXT.INITIAL_CONTEXT_FACTORY,INITCTX);
Env.put (Context.provider_url,my_host);
Env.put (Context.security_authentication, "simple"); Use simple authentication to authenticate users
Env.put (CONTEXT.SECURITY_PRINCIPAL,MGR_DN);
Env.put (CONTEXT.SECURITY_CREDENTIALS,MGR_PW);
DirContext CTX = new InitialDirContext (env);

Set query scope and start query
Searchcontrols constraints = new Searchcontrols ();
Constraints.setsearchscope (Searchcontrols.subtree_scope);
Namingenumeration results = Ctx.search (my_searchbase,my_filter,constraints);

Print Query Results
while (results!= null && results.hasmore ()) {
SearchResult sr = (SearchResult) results.next ();
String DN = Sr.getname ();
String DN = sr.getname () + "," +my_searchbase;
System.out.println ("==============================================");
System.out.println ("Distinguished Name is:" +DN);

Print the specified field//////////////////////////////////////////////////////////////////
Attributes ar = ctx.getattributes (dn,my_attrs);
if (ar==null) {
The corresponding UID has no extra attributes
System.out.println ("Entry" +dn+ "has none of the specified attributes\n");
} else {
Start showing the corresponding fields
for (int i=0;i<my_attrs.length;i++) {
Attribute attr = Ar.get (My_attrs[i]);
if (attr!=null) {
System.out.print (my_attrs[i]+ ":");
For (Enumeration vals = Attr.getall ();
Vals.hasmoreelements (); ) {
temp = (String) vals.nextelement ();
System.out.println ("T" +temp);
Res.append (temp+ "/");
}
}
System.out.println ("\ n");
}
///////////////////////////////////////////////////////////////////////////////////


/* Print all fields///////////////////////////////////////////////////////////////////
Attributes attrs = Sr.getattributes ();
for (namingenumeration ne = Attrs.getall ();
Ne.hasmoreelements (); ){
Attribute attr = (attribute) ne.next ();
String Attrid = Attr.getid ();
System.out.println (attrid+ ":");
For (Enumeration vals = Attr.getall (); vals.hasmoreelements ();) {
System.out.println ("T" +vals.nextelement ());
}
*//////////////////////////////////////////////////////////////////////////////////
}
}
}catch (Exception e) {
E.printstacktrace ();
System.exit (1);
}
System.out.println (res.tostring () + "\n\n\n\n");

Splitstring sp = new splitstring ();
System.out.println ("A Total" +sp.splitstring (res.tostring ()). length+ "a return"); Print display results, calculate returned array values
Return sp.splitstring (Res.tostring ());
return res.tostring ();

}

///////////////////////////////////////////////////////////////////////////////////////////
Use regular expressions to sort extracted strings///////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.