Java API 操作 OpenLDAP

OpenLDAP Windows 安装

@RunWith(SpringRunner.class)
@SpringBootTest
public class OpenLDAPJavaAPITest {

    private static Hashtable ev = new Hashtable();
    private static DirContext dirContext;

    static {
        ev.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        ev.put(Context.PROVIDER_URL, "ldap://127.0.0.1:389/dc=shpun,dc=com");
        ev.put(Context.SECURITY_AUTHENTICATION, "simple");
        ev.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=shpun,dc=com");
        ev.put(Context.SECURITY_CREDENTIALS, "secret");
        try {
            dirContext = new InitialDirContext(ev);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Java LDAP 查询 person cn属性
     */
    @Test
    public void getAllPersonNamesJavaLDAp() {
        List<String> list = new ArrayList<String>();
        NamingEnumeration results = null;
        try {
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            results = dirContext.search("", "(objectclass=inetOrgPerson)", controls);

            while (results.hasMore()) {
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();
                Attribute attr = attributes.get("cn");
                String cn = attr.get().toString();
                list.add(cn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (dirContext != null) {
                try {
                    dirContext.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * uid=zhangsan1001uid,ou=people,dc=shpun,dc=com 不行
     * uid=zhangsan1001uid,ou=people 可以
     */
    @Test
    public void getByDn() throws Exception{
        Object obj = dirContext.lookup("uid=zhangsan1001uid,ou=people");
    }

    /**
     * dirContext.search 的 name参数置空可以
     */
    @Test
    public void searchByAttribute() {
        NamingEnumeration results = null;
        try {
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String returnedAtts[] = { "cn","sn","description","title","uid","displayName" };
            controls.setReturningAttributes(returnedAtts);

            results = dirContext.search("ou=people", "uid=zhangsan1001uid", controls);
            while (results.hasMoreElements()) {
                SearchResult searchResult = (SearchResult)results.next();
                System.out.println(">>>" + searchResult.getName());
                Attributes attrs = searchResult.getAttributes();
                if (attrs != null) {
                    for (NamingEnumeration<? extends Attribute> names = attrs.getAll(); names.hasMore();) {
                        Attribute attr = names.next();
                        System.out.println("AttributeID: " + attr.getID());
                        for (NamingEnumeration<?> e = attr.getAll(); e.hasMore();) {
                            System.out.println("Attributes:" + e.next());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (dirContext != null) {
                try {
                    dirContext.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

	/**
     * 添加
     */
    @Test
    public void insert(){
        Attributes attrs = new BasicAttributes();
        attrs.put("uid", "zhangsan1006uid");
        attrs.put("sn", "zhangsan1006sn");
        attrs.put("cn", "zhangsan1006cn");
        attrs.put("description", "zhangsan1006description");
        attrs.put("title", "zhangsan1006title");
        attrs.put("displayName", "zhangsan1006displayName");

        Attribute objClass = new BasicAttribute("objectClass");
        objClass.add("top");
        objClass.add("person");
        objClass.add("organizationalPerson");
        objClass.add("inetOrgPerson");
        attrs.put(objClass);

        try {
            dirContext.createSubcontext("uid=zhangsan1006uid,ou=people", attrs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dirContext != null) {
                try {
                    dirContext.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
   	 * 更新单个属性
     * 需要登录验证,在ev的Hashtable中添加OpenLDAP的账号密码
     */
    @Test
    public void updateAttribute() {
        ModificationItem[] mods = new ModificationItem[1];
        Attribute attr = new BasicAttribute("cn", "zhangsan1006cnUpdate");
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);

        try {
            dirContext.modifyAttributes("uid=zhangsan1006uid,ou=people", mods);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dirContext != null) {
                try {
                    dirContext.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
	
	/**
     * 删除
     */
    @Test
    public void delete(){
        try {
            dirContext.destroySubcontext("uid=zhangsan1006uid,ou=people");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dirContext != null) {
                try {
                    dirContext.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

参考:
使用JAVA自带方法增删改查LDAP

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值