LDAP方式串連AD擷取使用者資訊

來源:互聯網
上載者:User

標籤:

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;
/**
* 初始化ldap
*/
public void initLdap(){
//ad伺服器
this.host = "xxx.com"; // AD伺服器
this.url = new String("ldap://" + host );//預設連接埠為80的可以不用填寫,其他連接埠需要填寫,如ldap://xxx.com:8080
this.adminName = "[email protected]";// 注意使用者名稱的寫法:domain\User 或 [email protected]
this.adminPassword = "admin";
Hashtable HashEnv = new Hashtable();
HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP訪問安全層級
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工廠類
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);
}
}
/**
* 關閉ldap
*/
public void closeLdap(){
try {
this.ctx.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param type organizationalUnit:組織架構 group:使用者組 user|person:使用者
* @param name
* @return
*/
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";
// LDAP搜尋過濾器類
//cn=*name*模糊查詢 cn=name 精確查詢
//String searchFilter = "(objectClass="+type+")";
String searchFilter = "(&(objectClass="+type+")("+filter+"=*" + name + "*))";
// 建立搜尋控制器
SearchControls searchCtls = new SearchControls();
// 設定搜尋範圍
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//String returnedAtts[] = { "memberOf" }; // 定製返回屬性
//searchCtls.setReturningAttributes(returnedAtts); // 設定返回屬性集 不設定則返回所有屬性
// 根據設定的域節點、過濾器類和搜尋控制器搜尋LDAP得到結果
NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);// Search for objects using the filter
// 初始化搜尋結果數為0
int totalResults = 0;// Specify the attributes to return
int rows = 0;
while (answer.hasMoreElements()) {// 遍曆結果集
SearchResult sr = (SearchResult) answer.next();// 得到符合搜尋條件的DN
++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);
}
}// if
}// while
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();
}
}

 

原文地址:http://wibiline.iteye.com/blog/1840739

LDAP方式串連AD擷取使用者資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.