zoukankan      html  css  js  c++  java
  • java操作ldap

    使用jndi连接ldap数据库,操作ldap条目

    1,连接ldap数据库:

    public DirContext createDirContext(String ip,int port,String bindDn,String password){
    
        Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://" + ip + ":" + port);
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, bindDn);
            env.put(Context.SECURITY_CREDENTIALS, password);
        DirContext ctx = null;
        try{
    
            ctx = new InitialDirContext(env);
        }catch(new InitialDirContext(env);){
    
             e.printStackTrace();
        }
        return ctx;
    }
            

    能否成功创建DirContext对象可以用来测试绑定用户密码是否正确,在进行ldap登陆测试时使用。

    2,创建条目

    public void addItem(String ip,int port,String bindDN, String password, String itemDn, HashMap<String, ArrayList<String>> attrValueMap)
                throws NamingException {
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://" + ip + ":" + port);
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, bindDN);
            env.put(Context.SECURITY_CREDENTIALS, password);
         DirContext ctx = null;
    try { ctx = new InitialDirContext(env); BasicAttributes entry = new BasicAttributes(true); Iterator<String> defaultAttrValueMapKeyIt = attrValueMap.keySet().iterator(); while (defaultAttrValueMapKeyIt.hasNext()) { String attr = defaultAttrValueMapKeyIt.next(); ArrayList<String> valueList = attrValueMap.get(attr); if (1 == valueList.size()) { entry.put(attr, valueList.get(0)); } else { Attribute attribute = new BasicAttribute(attr); for (String value : valueList) { attribute.add(value); } entry.put(attribute); } } ctx.createSubcontext(itemDn, entry);
          } catch (NamingException e){
            
    throw e;
          }
    finally{
            ctx.close();

           }

          }
    
    
    attrValueMap保存条目的所有属性信息,添加条目的属性要根据其对应的objectClass的必填属性填写,不能将必填属性设为空值,否则添加时会抛出异常,错误代码65。

    3,修改条目
    public void updateUser(String ip,int port,String bindDN, String password, String itemDn, HashMap<String, ModifyAttribute> modifyAttrMap)
                throws NamingException {
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://" + ip + ":" + port);
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, bindDn);
            env.put(Context.SECURITY_CREDENTIALS, password);
         DirContext ctx = null;
            try {
                ctx = new InitialDirContext(env);
                ModificationItem[] modificationItems = new ModificationItem[modifyAttrMap.size()];
    
                int i = 0;
                Iterator<String> it = modifyAttrMap.keySet().iterator();
                while (it.hasNext()) {
                    ModifyAttribute ma = modifyAttrMap.get(it.next());
                    modificationItems[i++] = new ModificationItem(ma.getType(), new BasicAttribute(ma.getAttr(), ma.getValue()));
                }
    
                ctx.modifyAttributes(itemDn, modificationItems);
    
            } catch (NamingException e) {
                throw e;
            }finally{    

          ctx.close();
          }

        }

    4,删除条目

    public void deleteUser(String ip,int port,String bindDn, String password, String itemDn) throws NamingException {
            if (null != itemDn && !itemDn.equals("")) {
    
                Hashtable<String, String> env = new Hashtable<String, String>();
                env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                env.put(Context.PROVIDER_URL, "ldap://" + ip + ":" + port);
                env.put(Context.SECURITY_AUTHENTICATION, "simple");
                env.put(Context.SECURITY_PRINCIPAL, bindDn);
                env.put(Context.SECURITY_CREDENTIALS, password);
           DirContext ctx = null; 
                try {
                    ctx = new InitialDirContext(env);
                    ctx.destroySubcontext(itemDn);
                } catch (NamingException e) {
                    throw e;
                }finally{
              ctx.close();
           }
            }
        }
     
  • 相关阅读:
    ecplise自动提示失效,使用补全自动提示快捷键(Alt+/),但只显示“No Default Proposals”
    maven构建ssh工程
    pom.xml中坐标的组成
    依赖传递的规则
    maven中导入包版本冲突的解决
    maven工程的拆分与聚合
    maven的生命周期
    maven的常用命令
    在pom.xml中引入jar包坐标的依赖范围
    【stl的神奇操作】用集合搞定区间相交
  • 原文地址:https://www.cnblogs.com/adam1991/p/7656415.html
Copyright © 2011-2022 走看看