📄 testad2.java
字号:
/*
* ActiveDirectoryFacility.java
*
* Created on January 30, 2007, 5:32 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Wearnes;
/**
*
* @author dongliang.guo
*/
import java.util.Hashtable;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import javax.naming.*;
public class TestAD2 {
public TestAD2()
{
// System.out.println("1:"+CommonUtility.getConnectionURL()+":");
}
public static void main(String[] args)
{
new TestAD2();
String uid="dongliang.guo";
String ldapHost="10.88.0.86";
Employee employee=null;
String name="";
String email="";
String company="";
String department="";
String defaultManager="";
String secondManager="";
String FC="";
String GC_PORT="3268";
String DC_PORT="389";
Hashtable envGC = new Hashtable();
Hashtable envDC = new Hashtable();
//String adminName = "CN=Administrator,CN=Users,DC=WEARNES,DC=COM";
String adminName = "Wearnes\\Administrator";
String adminPassword = "Rotartsinimda88;";
//Note the GC port; 3268, and the normal LDAP port 389
//Just for the hell of it, lets use a different GC and DC
String urlGC = "ldap://10.88.0.86:3268";
String urlDC = "ldap://10.88.0.86:389";
envGC.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
envDC.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
//set security credentials, note using simple cleartext authentication
envGC.put(Context.SECURITY_AUTHENTICATION,"simple");
envGC.put(Context.SECURITY_PRINCIPAL,adminName);
envGC.put(Context.SECURITY_CREDENTIALS,adminPassword);
envDC.put(Context.SECURITY_AUTHENTICATION,"simple");
envDC.put(Context.SECURITY_PRINCIPAL,adminName);
envDC.put(Context.SECURITY_CREDENTIALS,adminPassword);
//connect to both a GC and DC
envGC.put(Context.PROVIDER_URL,urlGC);
envDC.put(Context.PROVIDER_URL,urlDC);
//We need to chase referrals when retrieving attributes from the DC
//as the object may be in a different domain
envDC.put(Context.REFERRAL,"follow");
try {
//Create the initial directory context for both DC and GC
LdapContext ctxGC = new InitialLdapContext(envGC,null);
System.out.println("1");
LdapContext ctxDC = new InitialLdapContext(envDC,null);
//Now perform a search against the GC
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the attributes to return
String returnedAtts[]={"sn","givenName","mail","company","department"};
searchCtls.setReturningAttributes(returnedAtts);
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
String searchFilter = "(&(objectClass=user)(mail=*)(|(sn=guo)))";
//Specify the Base for the search
//an empty dn for all objects from all domains in the forest
String searchBase = "";
//initialize counter to total the results
int totalResults = 0;
//Search for objects in the GC using the filter
NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtls);
//Loop through the search results
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
totalResults++;
System.out.println(">>>" + sr.getName());
// Print out some of the attributes, catch the exception if the attributes have no values
/*
Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
name= attrs.get("givenName").get().toString() + " " + attrs.get("sn").get().toString();
email= attrs.get("mail").get().toString();
company= attrs.get("company").get().toString();
department= attrs.get("department").get().toString();
}
catch (NullPointerException e) {
System.err.println("Problem listing attributes from Global Catalog: " + e);
}
}*/
//Now retrieve attributes from the DC
Attributes DCattrs = ctxDC.getAttributes(sr.getName());
try {
name= DCattrs.get("givenName").get().toString() + " " + DCattrs.get("sn").get().toString();
email= DCattrs.get("mail").get().toString();
company= DCattrs.get("company").get().toString();
department= DCattrs.get("department").get().toString();
String description= DCattrs.get("description").get().toString();
String [] tmp=description.split(",");
defaultManager=tmp[0];
// secondManager=tmp[1];
// FC=tmp[2];
//System.out.println(" Fax(DC): " + DCattrs.get("facsimileTelephoneNumber").get());
String title= DCattrs.get("title").get().toString();
System.out.println("name:"+name);
}
catch (NullPointerException e) {
System.err.println("Problem listing attributes from Domain Controller: " + e);
}
}
System.out.println("Total results: " + totalResults);
ctxGC.close();
ctxDC.close();
//String test= SendMailBean.send("melody.shieh@wearnes.com","dongliang.guo@wearnes.com","dongliang.guo@wearnes.com" , "",
// "IT Request","please process the request", "sgtpl7ex3.wearnes.grp");
//System.err.println("test:"+CommonUtility.getConnectionURL());
System.out.println(":"+ActiveDirectoryFacility.getEmployeeProfile("rizal.wong","10.88.0.86").fullname+":");
// System.out.println((new CommonUtility()).getFC("WBS"));
}
catch (NamingException e) {
System.err.println("Problem searching directory: " + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -