LDAP way to connect AD to get user information

Source: Internet
Author: User
Tags ldap ldap search filter throw exception ad server

For an introduction to LDAP data, refer to: http://wenku.baidu.com/view/262742f9f705cc17552709f9.html

Errors in LDAP access to the ad domain are typically in the following format:

LDAP load Error: [Ldap:error code 49-80090308:ldaperr:dsid-0c090334, comment:acceptsecuritycontext error, data 525, Vece]

Where the scarlet letter part of the meaning is as follows (these error codes are not related to language):

525-User not found

52E-Incorrect Certificate

530-not permitted to logon @ this time

532-Expiry of password

533-Account Not available

701-Expiry of account

773-User must reset password

The cases are as follows:

Import java.util.Hashtable;
Import Javax.naming.Context;
Import javax.naming.NamingEnumeration;
Import javax.naming.NamingException;
Import Javax.naming.directory.Attribute;
Import javax.naming.directory.Attributes;
Import Javax.naming.directory.SearchControls;
Import Javax.naming.directory.SearchResult;
Import Javax.naming.ldap.InitialLdapContext;
Import Javax.naming.ldap.LdapContext;

public class Ldapadhelper {
Public Ldapadhelper () {
}
Private String Host,url,adminname,adminpassword;
Private Ldapcontext CTX = null;
/**
* Initialize LDAP
*/
public void Initldap () {
Ad Server
This.host = "xxx.com"; Ad Server
This.url = new String ("LDAP://" + host);//The default port is 80 can not fill, other ports need to fill, such as ldap://xxx.com:8080
This.adminname = "[email protected]";//Note the wording of the user name: domain\user or [email protected]
This.adminpassword = "admin";
Hashtable hashenv = new Hashtable ();
Hashenv.put (Context.security_authentication, "simple"); LDAP Access security level
Hashenv.put (Context.security_principal, AdminName); AD User
Hashenv.put (Context.security_credentials, AdminPassword); AD Password
Hashenv.put (Context.initial_context_factory, "com.sun.jndi.ldap.LdapCtxFactory"); LDAP Factory class
Hashenv.put (Context.provider_url, URL);
try {
CTX = new Initialldapcontext (hashenv, NULL);
SYSTEM.OUT.PRINTLN ("Initialize LDAP successfully! ");
} catch (Namingexception e) {
E.printstacktrace ();
System.err.println ("Throw Exception:" + e);
}
}
/**
* Close LDAP
*/
public void Closeldap () {
try {
This.ctx.close ();
} catch (Namingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
*
* @param type organizationalunit: organization structure Group: User group User|person: User
* @param name
* @return
*/
public string Getadinfo (string type, string filter, string name) {

String userName = name; User name
if (UserName = = null) {
UserName = "";
}
String company = "";
String result = "";
try {
Domain node
String searchbase = "dc=xx,dc=xxx,dc=com";
LDAP Search Filter Class
cn=*name* fuzzy query Cn=name exact query
String searchfilter = "(objectclass=" +type+ ")";
String Searchfilter = "(& (objectclass=" +type+ ") (" +filter+ "=*" + name + "*))";
Creating a Search Controller
Searchcontrols searchctls = new Searchcontrols ();
Set the search scope
Searchctls.setsearchscope (Searchcontrols.subtree_scope);
String returnedatts[] = {"MemberOf"}; Customizing return properties
Searchctls.setreturningattributes (Returnedatts); Sets the return property set to return all properties if not set
Search LDAP based on domain node, filter class, and search controller set to get results
Namingenumeration answer = Ctx.search (Searchbase, SEARCHFILTER,SEARCHCTLS);//Search for objects using the filter
The number of initialized search results is 0
int totalresults = 0;//Specify the attributes to return
int rows = 0;
while (Answer.hasmoreelements ()) {//Traversal result set
SearchResult sr = (SearchResult) answer.next ();//Get the DN that matches the search criteria
++rows;
String DN = Sr.getname ();
SYSTEM.OUT.PRINTLN (DN);
Attributes attrs = Sr.getattributes ();//Get the set of properties that match the condition
if (attrs! = null) {
try {
for (namingenumeration ne = Attrs.getall (); Ne.hasmore ();) {
Attribute Attr = (Attribute) ne.next ();//Get Next property
System.out.println ("Attributeid= Property Name:" + Attr.getid (). toString ());
Reading property values
for (namingenumeration e = Attr.getall (); E.hasmore (); totalresults++) {
Company = E.next (). toString ();
SYSTEM.OUT.PRINTLN ("attributevalues= attribute value:" + company);
}
System.out.println ("---------------");

}
} catch (Namingexception e) {
System.err.println ("Throw Exception:" + e);
}
}//if
}//while
System.out.println ("************************************************");
System.out.println ("Number:" + totalresults);
SYSTEM.OUT.PRINTLN ("Total number of users:" +rows);
} catch (Namingexception e) {
E.printstacktrace ();
System.err.println ("Throw Exception:" + e);
}
return result;
}

public static void Main (String args[]) {
Instantiation of
Ldapadhelper ad = new Ldapadhelper ();
Ad.initldap ();
Ad. Getadinfo ("user", "cn", "Li xx");//Find Users
Ad. Getadinfo ("organizationalunit", "ou", "engineering");//Find Organizational Structure
Ad. Getadinfo ("group", "cn", "Fujian xxx");//Find a user group

Ad.closeldap ();
}
}

Original address: http://wibiline.iteye.com/blog/1840739

LDAP way to connect AD to get user information

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.