服务器之家

服务器之家 > 正文

JAVA_基本LDAP操作实例

时间:2019-10-15 10:22     来源/作者:java之家

一、简介

Lightweight Directory Access Protocol (LDAP),轻型目录访问协议是一个访问在线目录服务的协议。下面的例子中简单介绍在java中队ldap的增删该查功能。目录结构为:

CD=CAS,DC=MYDC

--cn=users

----uid=zhangsan

二、示例
1、通过LdapContext连接ldap
 

复制代码代码如下:

/** 
 * 连接LDAP 
 */  
@SuppressWarnings({ "rawtypes", "unchecked" })  
public LdapContext connetLDAP() throws NamingException {  
    // 连接Ldap需要的信息  
    String ldapFactory = "com.sun.jndi.ldap.LdapCtxFactory";  
    String ldapUrl = "ldap:/IP:port";// url  
    String ldapAccount = "cn=root"; // 用户名  
    String ldapPwd = "password";//密码  
    Hashtable env = new Hashtable();  
    env.put(Context.INITIAL_CONTEXT_FACTORY, ldapFactory);  
    // LDAP server  
    env.put(Context.PROVIDER_URL, ldapUrl);  
    env.put(Context.SECURITY_AUTHENTICATION, "simple");  
    env.put(Context.SECURITY_PRINCIPAL, ldapAccount);  
    env.put(Context.SECURITY_CREDENTIALS, ldapPwd);  
    env.put("java.naming.referral", "follow");  
    LdapContext ctxTDS = new InitialLdapContext(env, null);  
    return ctxTDS;  
}  


2、增加用户zhangsan

复制代码代码如下:
  
// 添加  
    public void testAdd() throws Exception {  
        LdapContext ctx = connetLDAP();  
        Attributes attrs = new BasicAttributes(true);  
        Attribute objclass = new BasicAttribute("objectclass");  
        // 添加ObjectClass  
        String[] attrObjectClassPerson = { "inetOrgPerson", "organizationalPerson", "person", "top" };  
        Arrays.sort(attrObjectClassPerson);  
        for (String ocp : attrObjectClassPerson) {  
            objclass.add(ocp);  
        }  
        attrs.put(objclass);  
        String uid = "zhangsan";  
        String userDN = "uid=" + uid + "," + "cn=users,dc=cas,dc=mydc";  
        // 密码处理  
        // attrs.put("uid", uid);  
        attrs.put("cn", uid);  
        attrs.put("sn", uid);  
        attrs.put("displayName", "张三");  
        attrs.put("mail", "abc@163.com");  
        attrs.put("description", "");  
        attrs.put("userPassword", "Passw0rd".getBytes("UTF-8"));  
        ctx.createSubcontext(userDN, attrs);  
    }  


3、删除用户zhangsan

复制代码代码如下:

//删除  
    public void testRemove() throws Exception {  
        LdapContext ctx = connetLDAP();  
        String uid = "zhangsan";  
        String userDN = "uid=" + uid + "," + "cn=users,dc=cas,dc=mydc";  
        ctx.destroySubcontext(userDN);  

    }  


4、修改zhangsan的邮件地址

复制代码代码如下:
  
//修改  
    public boolean testModify() throws Exception {  
        boolean result = true;  
        LdapContext ctx = connetLDAP();  
        String uid = "zhangsan";  
        String userDN = "uid=" + uid + "," + "cn=users,dc=cas,dc=mydc";  
        Attributes attrs = new BasicAttributes(true);  
        attrs.put("mail", "zhangsan@163.com");  
        ctx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs);  
        return result;  

    }  


5、查找用户

复制代码代码如下:

//查询  
    public void testSearch() throws Exception {  
        LdapContext ctx = connetLDAP();  
        // 设置过滤条件  
        String uid = "zhangsan";  
        String filter = "(&(objectClass=top)(objectClass=organizationalPerson)(uid=" + uid + "))";  
        // 限制要查询的字段内容  
        String[] attrPersonArray = { "uid", "userPassword", "displayName", "cn", "sn", "mail", "description" };  
        SearchControls searchControls = new SearchControls();  
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);  
        // 设置将被返回的Attribute  
        searchControls.setReturningAttributes(attrPersonArray);  
        // 三个参数分别为:  
        // 上下文;  
        // 要搜索的属性,如果为空或 null,则返回目标上下文中的所有对象;  
        // 控制搜索的搜索控件,如果为 null,则使用默认的搜索控件  
        NamingEnumeration<SearchResult> answer = ctx.search("cn=users,dc=cas,dc=mydc", filter.toString(), searchControls);  
        // 输出查到的数据  
        while (answer.hasMore()) {  
            SearchResult result = answer.next();  
            NamingEnumeration<? extends Attribute> attrs = result.getAttributes().getAll();  
            while (attrs.hasMore()) {  
                Attribute attr = attrs.next();  
                System.out.println(attr.getID() + "=" + attr.get());  
            }  
            System.out.println("============");  
        }  
    }  
 
标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部