LDAP资料介绍可以参考:http://wenku.baidu.com/view/262742f9f705cc17552709f9.html
ldap访问AD域的的错误一般会如下格式:
Ldap load error: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 525, vece]
其中红字部分的意思如下(这些错误码跟语言无关):
525 - 用户没有找到
52e - 证书不正确
530 - not permitted to logon at this time
532 - 密码期满
533 - 帐户不可用
701 - 账户期满
773 - 用户必须重设密码
- 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;
-
-
-
- public void initLdap(){
-
- this.host = "xxx.com";
- this.url = new String("ldap://" + host );//默认端口为80的可以不用填写,其他端口需要填写,如ldap://xxx.com:8080
- this.adminName = "admin@xxx.com";
- this.adminPassword = "admin";
- Hashtable HashEnv = new Hashtable();
- HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
- HashEnv.put(Context.SECURITY_PRINCIPAL, adminName);
- HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword);
- HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
- HashEnv.put(Context.PROVIDER_URL, url);
- try {
- ctx = new InitialLdapContext(HashEnv, null);
- System.out.println("初始化ldap成功!");
- } catch (NamingException e) {
- e.printStackTrace();
- System.err.println("Throw Exception : " + e);
- }
- }
-
-
-
- public void closeLdap(){
- try {
- this.ctx.close();
- } catch (NamingException e) {
-
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
- public String GetADInfo(String type ,String filter ,String name) {
-
- String userName = name;
- if (userName == null) {
- userName = "";
- }
- String company = "";
- String result = "";
- try {
-
- String searchBase = "DC=xx,DC=xxx,DC=com";
-
-
-
- String searchFilter = "(&(objectClass="+type+")("+filter+"=*" + name + "*))";
-
- SearchControls searchCtls = new SearchControls();
-
- searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
-
-
-
- NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);
-
- int totalResults = 0;
- int rows = 0;
- while (answer.hasMoreElements()) {
- SearchResult sr = (SearchResult) answer.next();
- ++rows;
- String dn = sr.getName();
- System.out.println(dn);
- Attributes Attrs = sr.getAttributes();
- if (Attrs != null) {
- try {
- for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {
- Attribute Attr = (Attribute) ne.next();
- System.out.println(" AttributeID=属性名:"+ Attr.getID().toString());
-
- for (NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++) {
- company = e.next().toString();
- System.out.println(" AttributeValues=属性值:"+ company);
- }
- System.out.println(" ---------------");
-
- }
- } catch (NamingException e) {
- System.err.println("Throw Exception : " + e);
- }
- }
- }
- System.out.println("************************************************");
- System.out.println("Number: " + totalResults);
- System.out.println("总共用户数:"+rows);
- } catch (NamingException e) {
- e.printStackTrace();
- System.err.println("Throw Exception : " + e);
- }
- return result;
- }
-
- public static void main(String args[]) {
-
- LdapADHelper ad = new LdapADHelper();
- ad.initLdap();
- ad.GetADInfo("user","cn","李XX");
- ad.GetADInfo("organizationalUnit","ou","工程");
- ad.GetADInfo("group","cn","福建xxx");
-
- ad.closeLdap();
- }
- }