JAVA LADP AD 도 메 인 사용자 목록 정보 가 져 오기
import java.util.Enumeration;
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 ADOperTest {
public ADOperTest() {
}
public void GetADInfo() {
String userName = "xxxx"; //
String passwd = "xxx";
String host = "192.168.100.14"; // AD
String port = "389"; //
String domain = "@xxx.com"; //
String url = new String("ldap://" + host + ":" + port);
String user = userName.indexOf(domain) > 0 ? userName : userName
+ domain;
Hashtable HashEnv = new Hashtable();
// String adminName ="CN=oyxiaoyuanxy,CN=Users,DC=Hebmc,DC=com";//AD
String adminName = "xueqiang"; // :domain\User
// [email protected]
adminName = "xueqiang"; // :domain\User [email protected]
String adminPassword = "123456"; //
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 {
LdapContext ctx = new InitialLdapContext(HashEnv, null);
//
String searchBase = "DC=wanda-dev,DC=cn";
// LDAP
String searchFilter = "objectClass=User";
//
SearchControls searchCtls = new SearchControls(); // Create the
// search
// controls
//
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify
// the
// search
// scope
//
// searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE); //
// Specify the search scope
String returnedAtts[] = { "memberOf", "distinguishedName",
"Pwd-Last-Set", "User-Password", "cn" };//
// String returnedAtts[] = { "url", "whenChanged", "employeeID",
// "name", "userPrincipalName", "physicalDeliveryOfficeName",
// "departmentNumber", "telephoneNumber", "homePhone",
// "mobile", "department", "sAMAccountName", "whenChanged",
// "mail" }; //
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
System.out.println(++rows
+ "************************************************");
System.out.println(sr.getName());
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++) {
System.out.println(" AttributeValues= :"
+ e.next().toString());
}
System.out.println(" ---------------");
//
// Enumeration values = Attr.getAll();
// if (values != null) { //
// while (values.hasMoreElements()) {
// System.out.println(" AttributeValues= :"+
// values.nextElement());
// }
// }
// System.out.println(" ---------------");
}
} catch (NamingException e) {
System.err.println("Throw Exception : " + e);
}
}
}
System.out
.println("************************************************");
System.out.println("Number: " + totalResults);
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
System.err.println("Throw Exception : " + e);
}
}
/* public static void main(String args[]) {
//
ADOperTest ad = new ADOperTest();
ad.GetADInfo();
}*/
public static void main(String args[]) {
Hashtable HashEnv = new Hashtable();
String LDAP_URL = "ldap://xxx.com:389"; //LDAP
String adminName = "[email protected]"; // :domain\User [email protected]
String adminPassword = "xxxx"; //
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, LDAP_URL);
try {
LdapContext ctx = new InitialLdapContext(HashEnv, null);
SearchControls searchCtls = new SearchControls(); //Create the search controls
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); //Specify the search scope
String searchFilter = "objectClass=User"; //specify the LDAP search filter
String searchBase = "DC=xxx,DC=com"; //Specify the Base for the search//
int totalResults = 0;
String returnedAtts[] = {
"url", "whenChanged", "employeeID", "name", "userPrincipalName",
"physicalDeliveryOfficeName", "departmentNumber", "telephoneNumber",
"homePhone", "mobile", "department", "sAMAccountName", "whenChanged",
"mail"}; //
searchCtls.setReturningAttributes(returnedAtts); //
//Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);
if(answer==null||answer.equals(null)){
System.out.println("answer is null");
}else{
System.out.println("answer not null");
}
System.out.println(answer.hasMoreElements());
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
System.out.println("************************************************");
System.out.println(sr.getName());
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++) {
System.out.println(" AttributeValues=" + e.next().toString());
}
System.out.println(" ---------------");
//
Enumeration values = Attr.getAll();
if (values != null) { //
while (values.hasMoreElements()) {
System.out.println(" AttributeValues=" + values.nextElement());
}
}
System.out.println(" ---------------");
}
}
catch (NamingException e) {
System.err.println("Throw Exception : " + e);
}
}
}
System.out.println("Number: " + totalResults);
ctx.close();
}
catch (NamingException e) {
e.printStackTrace();
System.err.println("Throw Exception : " + e);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.