码迷,mamicode.com
首页 > 编程语言 > 详细

java测试类调用LDAP服务器客户端信息实行增删改查

时间:2015-08-27 13:40:07      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:java   class   ldap   服务器   测试   

如下是我自写的java测试类调用客户端配置好的用户信息:

package com;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class UserAuthenticate {
    private String URL = "ldap://localhost:10389";
    private String BASEDN = "cn=zhangsan,ou=users,ou=system";
    private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private LdapContext ctx = null;
    private Hashtable env = null;
    private Control[] connCtls = null;

    @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
    public void LDAP_connect() {
        env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
        env.put(Context.PROVIDER_URL, URL);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=lisi;ou=users;ou=system");
        env.put(Context.SECURITY_CREDENTIALS, "123456");
        // env.put("", "secret");
        try {
            ctx = new InitialLdapContext(env, connCtls);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public void getUserDN() {
        String dn = "zhangsan";
        SearchControls controls = new SearchControls();
        //限制要查询的字段内容
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        //设置过滤条件
        String filter = "(&(objectClass=top)(objectClass=person)(cn=" + dn
                + "))";
        //设置被返回的attribute
        controls.setReturningAttributes(new String[] { "uid", "userPassword",
                "displayName", "cn", "sn", "mail", "description" });
        try {
            //控制搜索的搜索条件,如果为null则使用默认的搜索控件,要搜索的属性如果为null则返回目标上下文中的所有对象
            NamingEnumeration answer = ctx
                    .search("ou=system", filter, controls);
            while (answer.hasMore()) {
                SearchResult result = (SearchResult) answer.next();
                NamingEnumeration en = result.getAttributes().getAll();
                if (en == null) {
                    System.out.println("Have no NamingEnumeration");
                }
                if (!en.hasMoreElements()) {
                    System.out.println("Have no element");
                }
                //输出查询到的结果
                while (en.hasMore()) {
                    Attribute attr = (Attribute) en.next();
                    System.out.println(attr.getID() + "=" + attr.get());
                }
            }

        } catch (NamingException e) {
            e.printStackTrace();
        }

    }

    public void testAdd() throws Exception {
        Attributes attrs = new BasicAttributes(true);
        Attribute objclass = new BasicAttribute("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 + "," + "ou=system";
        attrs.put("cn", uid);
        attrs.put("sn", uid);
        attrs.put("displayName", "张三");
        attrs.put("description", "not null");
        attrs.put("mail", "abc@126.com");
        attrs.put("userPassword", "11111".getBytes("UTF-8"));
        ctx.createSubcontext(userDN, attrs);
    }
    public void testDelete(){
        String uid = "zhangsan";
        String userDN = "uid="+ uid +","+"ou=system";
        try {
            ctx.destroySubcontext(userDN);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
    public boolean testEdit(){
        boolean result = true;
        String uid = "zhangsan";
        String userDN = "uid="+ uid +","+"ou=system";
        Attributes attr = new BasicAttributes(true);
        attr.put("mail","zhangsan@163.com");
        try {
            ctx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attr);
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void main(String[] args) {
        UserAuthenticate test = new UserAuthenticate();
        // test.getUserDN("zhangsan;ou=users;ou=system");
        try {
            test.LDAP_connect();
             //test.testAdd();
            test.getUserDN();
            //test.testEdit();
            //test.testDelete();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

java测试类调用LDAP服务器客户端信息实行增删改查

标签:java   class   ldap   服务器   测试   

原文地址:http://blog.csdn.net/danruoshui315/article/details/48024003

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!