⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 activedirectoryfacility.java

📁 provide a room booking system
💻 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 ActiveDirectoryFacility	{
    /*
     uid is user login id 
     *
     */
  public static Employee getEmployeeProfile(String uid,String ldapHost)
  {
      
                
                 Employee employee=null;  
                 String name="";
                 String email="";
                 String company="";
                 String department="";
                 String title="";
                 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;";
                
                  String adminName = "Wearnes\\dongliang.guo";
		String adminPassword = "19740412";
 
		//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://"+ldapHost+":"+GC_PORT;
		String urlDC = "ldap://"+ldapHost+":"+DC_PORT;
 
		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);
                        
			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[]={"cn","displayName","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)(sAMAccountName="+uid+"))";
 
			//Specify the Base for the search
			//an empty dn for all objects from all domains in the foresst
			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
			if (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();
                                                title= attrs.get("title").get().toString();
                                               // System.out.println("  Manager,FC(GC): " + attrs.get("description").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();
					name= DCattrs.get("displayName").get().toString() ;
				
                                        email= DCattrs.get("mail").get().toString();
                                        if(DCattrs.get("company").get()!=null)
                                             company= DCattrs.get("company").get().toString();
                                        else
                                            company="";
                                        if(DCattrs.get("department").get()!=null)                                            
                                          department= DCattrs.get("department").get().toString();
                                        else
                                            department="";
                                        
                                        String description="";
                                        if(DCattrs.get("description").get()!=null)       
                                        {
                                            description= DCattrs.get("description").get().toString();                                            
                                            //String [] tmp=description.split(",");
                                            defaultManager=description;
                                            secondManager="";
                                            FC="";
                                        }
                                        else
                                            description="";
                                            
					//System.out.println("        Fax(DC): " + DCattrs.get("facsimileTelephoneNumber").get());
                                        if( DCattrs.get("title").get()!=null)
                                            title= DCattrs.get("title").get().toString(); 
                                        else
                                            title="";
                                }
				catch (NullPointerException e)	{
					System.err.println("Problem listing attributes from Domain Controller: " + e);
				}
                                employee=new Employee(uid,company,department,defaultManager,"",email,title,"",name,FC);
           
			}
 
	 		System.out.println("Total results: " + totalResults);
			ctxGC.close();
			ctxDC.close();
 
		} 
		catch (NamingException e) {
			System.err.println("Problem searching directory: " + e);
		}
    
    
          return employee;
    
  
  }
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -