JAVA操作LDAP交互

首先定义全局的ldap的IP地址等 看截图  ↓P1j#yZ@Gg*AE

开启ldap服务, 每次进行curd操作都需开服务 类似于jdbc那种

    /** 关闭Ldap连接     */
    public void close() {
        if (dc != null) {
            try {
                dc.close();
            } catch (NamingException e) {
                System.out.println("NamingException in close():" + e);
            }
        }
    }

/** 添加 */
    public static void add() {
        try {
            
            Attributes attrs = new BasicAttributes(true);
            Attribute objclass = new BasicAttribute("objectclass");
            
            //目录名称 或者 成员名称
            String newUserName = "AI技术部门";
            
            /**
             * 创建组
             */
            String[] attrObjectClassPerson = {"group","top" };
            Arrays.sort(attrObjectClassPerson);
            for (String ocp : attrObjectClassPerson) {
                objclass.add(ocp);
            }
            attrs.put(objclass);
            
            String userDN = "CN=" + newUserName + "," + "OU=人力资源,OU=集团,OU=HQ,DC=iamtest,DC=com";
            int UF_PASSWD_NOTREQD = 0x0020;
            int UF_NORMAL_ACCOUNT = 0x0200;
            int UF_DONT_EXPIRE_PASSWD = 0x10000;
            
            attrs.put("name",newUserName);
            attrs.put("cn",newUserName);
            attrs.put("objectCategory","CN=Group,CN=Schema,CN=Configuration,DC=iamtest,DC=com");
            attrs.put("distinguishedName","CN=AI技术部门,OU=人力资源,OU=集团,OU=HQ,DC=iamtest,DC=com");
            
            /**
             * 创建组内成员
             */
            /*String[] attrObjectClassPerson = { "user", "organizationalPerson", "person", "top" };
            Arrays.sort(attrObjectClassPerson);
            for (String ocp : attrObjectClassPerson) {
                objclass.add(ocp);
            }
            attrs.put(objclass);
            
            attrs.put("sn", "AI");
            attrs.put("givenName", "nbAI");
            attrs.put("cn", newUserName);
            attrs.put("displayName", newUserName);
            attrs.put("mail", "ramsdjs@163.com");
            attrs.put("description", "test1");
            attrs.put("userPrincipalName", "ranhdb@wilcom.com.cn");
            attrs.put("sAMAccountName", newUserName);
            attrs.put("msDS-SupportedEncryptionTypes", "0");
            attrs.put("facsimileTelephoneNumber", "1232342");
            attrs.put("pager", "****");
            attrs.put("ipPhone", "****");
            attrs.put("homePhone", "********");
            attrs.put("mobile", "***********");
            attrs.put("userAccountControl",Integer.toString(UF_DONT_EXPIRE_PASSWD + UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD)); */
            
            /**
             * 创建单个ou 目录
             */
            /*BasicAttributes attrs = new BasicAttributes();
            BasicAttribute objclassSet = new BasicAttribute("objectClass");
            
            objclassSet.add("organizationalUnit");
            objclassSet.add("top");
            
            attrs.put(objclassSet);
            attrs.put("ou", "permissions group");
            
            dc.createSubcontext("ou=permissions group," + "ou=people,DC=iamtest,DC=com",attrs);*/
            
            dc.createSubcontext(userDN, attrs); 
            System.out.println("新增节点目录成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception in add():" + e);
        }
    }

/**
     * 修改  
     * @throws Exception
     */
    public static void testModify() throws Exception {  
            String uid = "TestNB";  
            String userDN = "cn=" + uid + "," + "ou=people,dc=iamtest,dc=com";  
            Attributes attrs = new BasicAttributes(true);  
            attrs.put("mail", "mx_cjsbo48@163.com");  
            dc.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs);  
            System.out.println("修改成功!!!");
    }  

  /** 查询
    * @param base :根节点(在这里是"dc=example,dc=com")
    * @param scope :搜索范围,分为"base"(本节点),"one"(单层),""(遍历)
    * @param filter :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点)
    */
    public static void searchInformation() {
            String base = "ou=people,DC=iamtest,DC=com";
            String scope = "";
            String filter = "(&(objectClass=top)(objectClass=organizationalPerson)(cn=TestNB))";
            //String filter = "objectClass=*";
            SearchControls sc = new SearchControls();
            if (scope.equals("base")) {
                sc.setSearchScope(SearchControls.OBJECT_SCOPE);
            } else if (scope.equals("one")) {
                sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
            } else {
                sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
            }
            NamingEnumeration ne = null;
            try {
                ne = dc.search(base, filter, sc);
                
                while (ne.hasMore()) {
                    System.out.println();
                    SearchResult sr = (SearchResult) ne.next();
                    String name = sr.getName();
                    if (base != null && !base.equals("")) {
                        System.out.println("entry: " + name + "," + base);
                    } else {
                        System.out.println("entry: " + name);
                    }
              
                    Attributes at = sr.getAttributes();
                    NamingEnumeration ane = at.getAll();
                
                    while (ane.hasMore()) {
                        
                        Attribute attr = (Attribute) ane.next();
                        String attrType = attr.getID();
                        NamingEnumeration values = attr.getAll();
                        Vector vals = new Vector();
                        
                        while (values.hasMore()) {
                            Object oneVal = values.nextElement();
                        if (oneVal instanceof String) {
                            System.out.println(attrType + ": " + (String) oneVal);
                        } else {
                            System.out.println(attrType + ": " + new String((byte[]) oneVal));
                            }
                        }
                    }
                }
            } catch (Exception nex) {
                System.err.println("Error: " + nex.getMessage());
                nex.printStackTrace();
            }
    }

  /** 删除  */
    public static void delete() {
        String dn = "ou=people,dc=iamtest,dc=com";
        try {
            dc.destroySubcontext(dn);
            System.out.println("移除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception in delete():" + e);
        }
    }

  废话真多 直接上代码 真好1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值