zoukankan      html  css  js  c++  java
  • 通过LDAP在AD域控上进行添加、删除、修改、查询等各种操作。

    LDAP操作代码样例  初始化LDAP 目录服务上下文
    该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ldap://localhost:8389),认证方式采用simple类型,即用户名/密码方式。

    private static void initialContext() throws NamingException{
       if(singleton == null){
        singleton = new LDAPConnection();
        /*
        *
    在实际编码中,这些环境变量应尽可能通过配置文件读取
        */
        //LDAP
    服务地址
        singleton.sLDAP_URL = "ldap://localhost:8389";
        //
    管理员账号
        singleton.sMANAGER_DN = "uid=linly,ou=People,dc=jsoso,dc=net";
        //
    管理员密码
        singleton.sMANAGER_PASSWORD = "coffee";
        //
    认证类型
        singleton.sAUTH_TYPE = "simple";
        //JNDI Context
    工厂类
        singleton.sCONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
      
        singleton.envProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, singleton.sCONTEXT_FACTORY);
        singleton.envProps.setProperty(Context.PROVIDER_URL, singleton.sLDAP_URL);
        singleton.envProps.setProperty(Context.SECURITY_AUTHENTICATION, singleton.sAUTH_TYPE);
        singleton.envProps.setProperty(Context.SECURITY_PRINCIPAL, singleton.sMANAGER_DN);
        singleton.envProps.setProperty(Context.SECURITY_CREDENTIALS, singleton.sMANAGER_PASSWORD);
        /*
        *
    绑定ldap服务器
        */
        singleton.dirCtx = new InitialDirContext(singleton.envProps);
       }
    }

    通过一个Hashtable或者Properties对象为LDAPContext设置参数,而后初始化InitialDirContext,即可绑定LDAP服务。这相当于JDBC中获取数据库的Connection对象。

    绑定/创建LDAP条目对象
    用户可以使用bind方法创建新的LDAP条目,下面的代码创建一个DN"ou=Employee , dc=jsoso ,dc=net"OrganizationUnitLDAP条目如下:


    public boolean createOrganizationUnit(){
       String ldapGroupDN = "ou=Employee , dc=jsoso ,dc=net";
       try {
        /*
        *
    查找是否已经存在指定的OU条目
        *
    如果存在,则打印OU条目的属性信息
        *
    如果不存在,则程序会抛出NamingException异常,进入异常处理
        */
        Attributes attrs = dirContext.getAttributes(ldapGroupDN);
        System.out.println("Find the group , attributes list :");
        NamingEnumeration<String> nEnum = attrs.getIDs();  
        for( ; nEnum.hasMore() ; ){
         String attrID = nEnum.next();
         Attribute attr = (Attribute)attrs.get(attrID);
         System.out.println(attr.toString());
        }  
        return false;
       } catch (NamingException e) {
        /*
        *
    没有找到对应的Group条目,新增Group条目
        */
        //
    创建objectclass属性
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalunit");
        //
    创建cn属性
        Attribute cn = new BasicAttribute("ou", "Employee");
        //
    创建Attributes,并添加objectclasscn属性
        Attributes attrs = new BasicAttributes();
        attrs.put(objclass);
        attrs.put(cn);
        //
    将属性绑定到新的条目上,创建该条目
        try {
         dirContext.bind(ldapGroupDN, null, attrs);
         System.out.println("Group created successful");
         return true;
        } catch (NamingException e1) {
         e1.printStackTrace();
        }   
       }
       return false;
    }



    获取条目属性
    下面一段代码获取entryDN参数指定条目中的属性集合,并打印到控制台

    /**
    *
    获取一个指定的LDAP Entry
    * @param entryDN
    */
    public void find(String entryDN){
       try {
        Attributes attrs = dirContext.getAttributes(entryDN);
        if (attrs != null) {
         NamingEnumeration<String> nEnum = attrs.getIDs();
         for( ; nEnum.hasMore() ; ){
          String attrID = nEnum.next();
          Attribute attr = (Attribute)attrs.get(attrID);
          System.out.println(attr.toString());
         }
         System.out.println();
        }else{
         System.out.println("No found binding.");
        }
       }catch(NamingException ne) {
        ne.printStackTrace();
       }
    }

    修改条目属性
    修改DN=user.getDistinguishedName()的条目中的cngivennamesnuserpassword四个属性值。
    (注:参数DirContext.REPLACE_ATTRIBUTE有另外两个常量:DirContext.ADD_ATTRIBUTEDirContext.REMOVE_ATTRIBUTE,分别表示新增属性和删除属性。)

    /**
    *
    修改用户信息
    * @param user
    * @return
    * @throws Exception
    */
    public boolean modifyUser(LDAPUser user) throws Exception {
       //
    用户对象为空
       if (user == null) {
        throw new Exception("No user information!n");
       }

       //
    检查uid
       String userDN = user.getDistinguishedName();
       if (userDN == null && userDN.length() == 0) {
        throw new NamingException("No userDN you specify!n");
       }

       //
    判断用户条目是否已经存在
       if(!isUserexist(userDN)){
        return false;
       }
     
       //
    设置属性
       Attributes attrs = new BasicAttributes();
       setBasicAttribute(attrs, "cn", user.getCommomName());
       setBasicAttribute(attrs, "givenname", user.getFirstName());
       setBasicAttribute(attrs, "sn", user.getLastName());
       setBasicAttribute(attrs, "userpassword", user.getPassword());
       //
    修改属性
       try{
        dirContext.modifyAttributes(user.getDistinguishedName(),DirContext.REPLACE_ATTRIBUTE, attrs);
        System.out.println("User(" + user.getDistinguishedName() + ") information modified.n");
        return true;
       }catch(NamingException ne){
        ne.printStackTrace();
       }
       return false;
    }




    根据属性集搜索条目
    根据属性集matchingAttributes中的匹配值,在上下文DN= "ou=People,dc=jsoso ,dc=net"中搜索它的所有子树中的匹配条目。
    (注:SearchControlsSCOPE参数详见SearchControls SCOPE补充说明)

              /**
    *
    通过属性搜索LDAP范例
    * @return
    */
    public void searchByAttribute(Attributes matchingAttributes){
       String baseDN = "ou=People,dc=jsoso ,dc=net";
       SearchControls cons = new SearchControls();
       cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
       try {
        Name baseName = new LdapName(baseDN);
        NamingEnumeration<SearchResult> ne = dirContext.search(baseName, matchingAttributes);
        SearchResult entry = null;
        for(;ne.hasMore();){
         entry = ne.next();
         showEntry(entry);
        }    
       } catch (NamingException e) {
        e.printStackTrace();
       }
    }

    根据过滤器搜索条目
    根据过滤器条件,在上下文DN = "ou=People,dc=jsoso ,dc=net"中,搜索它的所有子树中的匹配条目。
    (注:过滤器filter的相关语法详见LDAP filter语法补充说明)

    /**
    *
    通过过滤器搜索LDAP范例
    * @return
    */
    public void searchByFilter(String filter){
       String baseDN = "ou=People,dc=jsoso ,dc=net";  
       SearchControls cons = new SearchControls();
       cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
       try {
        NamingEnumeration<SearchResult> ne = dirContext.search(baseDN, filter , cons);
        SearchResult entry = null;
        for(;ne.hasMore();){
         entry = ne.next();
         showEntry(entry);
        }    
       } catch (NamingException e) {
        e.printStackTrace();
       } 

     

    这里的内容是抄录别人的,自己写的没有别人写的这份全。这里的增加用户,增加组织单元,查找用户都经过了我的验证,没有问题。但是修改我没有验证通过。

    删除没有做,但是从API上看,是没有问题的。 详细内容可以去百度文库搜:LDAP实用资料收录3.doc 。

    该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ldap://localhost:8389),认证方式采用simple类型,即用户名/密码方式。

    Java代码 <!--[if !vml]-->复制代码<!--[endif]-->

    <!--[if !supportLists]-->1.   <!--[endif]-->private static void initialContext() throws NamingException{   

    <!--[if !supportLists]-->2.   <!--[endif]-->    if(singleton == null){   

    <!--[if !supportLists]-->3.   <!--[endif]-->        singleton = new LDAPConnection();   

    <!--[if !supportLists]-->4.   <!--[endif]-->        /*  

    <!--[if !supportLists]-->5.   <!--[endif]-->         * 在实际编码中,这些环境变量应尽可能通过配置文件读取  

    <!--[if !supportLists]-->6.   <!--[endif]-->         */  

    <!--[if !supportLists]-->7.   <!--[endif]-->        //LDAP服务地址   

    <!--[if !supportLists]-->8.   <!--[endif]-->        singleton.sLDAP_URL = "ldap://localhost:8389";    

    <!--[if !supportLists]-->9.   <!--[endif]-->        //管理员账号   

    <!--[if !supportLists]-->10.<!--[endif]-->        singleton.sMANAGER_DN = "uid=linly,ou=People,dc=jsoso,dc=net";   

    <!--[if !supportLists]-->11.<!--[endif]-->        //管理员密码   

    <!--[if !supportLists]-->12.<!--[endif]-->        singleton.sMANAGER_PASSWORD = "coffee";   

    <!--[if !supportLists]-->13.<!--[endif]-->        //认证类型   

    <!--[if !supportLists]-->14.<!--[endif]-->        singleton.sAUTH_TYPE = "simple";   

    <!--[if !supportLists]-->15.<!--[endif]-->        //JNDI Context工厂类   

    <!--[if !supportLists]-->16.<!--[endif]-->        singleton.sCONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";    

    <!--[if !supportLists]-->17.<!--[endif]-->           

    <!--[if !supportLists]-->18.<!--[endif]-->        singleton.envProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, singleton.sCONTEXT_FACTORY);   

    <!--[if !supportLists]-->19.<!--[endif]-->        singleton.envProps.setProperty(Context.PROVIDER_URL, singleton.sLDAP_URL);   

    <!--[if !supportLists]-->20.<!--[endif]-->        singleton.envProps.setProperty(Context.SECURITY_AUTHENTICATION, singleton.sAUTH_TYPE);   

    <!--[if !supportLists]-->21.<!--[endif]-->        singleton.envProps.setProperty(Context.SECURITY_PRINCIPAL, singleton.sMANAGER_DN);   

    <!--[if !supportLists]-->22.<!--[endif]-->        singleton.envProps.setProperty(Context.SECURITY_CREDENTIALS, singleton.sMANAGER_PASSWORD);   

    <!--[if !supportLists]-->23.<!--[endif]-->        /*  

    <!--[if !supportLists]-->24.<!--[endif]-->         * 绑定ldap服务器  

    <!--[if !supportLists]-->25.<!--[endif]-->         */  

    <!--[if !supportLists]-->26.<!--[endif]-->        singleton.dirCtx = new InitialDirContext(singleton.envProps);   

    <!--[if !supportLists]-->27.<!--[endif]-->    }   

    <!--[if !supportLists]-->28.<!--[endif]-->}  

  • 相关阅读:
    Xamarin和微软发起.NET基金会
    迷你 MVC
    编制进度计划、保存基准
    JQuery UI Layout Plug-in布局
    (转载)Log4Net 在多层项目中的使用小记
    Json.Net6.0
    EasyUI搭建前端框架
    using和yield return
    ExpandoObject,DynamicObject,DynamicMetaObject
    Net 4.0 之 Dynamic 动态类型
  • 原文地址:https://www.cnblogs.com/qlong8807/p/2766226.html
Copyright © 2011-2022 走看看