openLDAP win安装部署及java CRUD接口

目录

概述
测试环境
安装过程
配置启动
客户端介绍
多级DC的ldif文件的配置
java接口编写测试

[一]、概述

什么叫LDAP呢,概念的东西这里就不多讲了,网上搜索下有很多,本文的重点是介绍如何在windows平台上安装和配置openLDAP软件。

openLDAP官方网站:http://www.openldap.org/

[二]、测试环境

window10 – 64位
openLDAP 版本:2.4.49
官网下载地址:https://www.maxcrc.de/download/
本人下载地址:https://www.maxcrc.de/wp-content/uploads/2020/04/OpenLDAPforWindows_x86.zip

[三]、安装过程

按照提示 一直 next ,直到安装完成:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

安装完成后,在系统服务中,找到 OpenLDAP Service,先停止服务,再把启动类型修改成手动,便于自己的测试。

[四]、配置启动

安装目录:C:\OpenLDAP

编辑文件:C:\OpenLDAP\slapd.conf 找到如下内容:
1 suffix"dc=maxcrc,dc=com"
2 rootdn “cn=Manager,dc=maxcrc,dc=com”

 suffix"dc=maxcrc,dc=com"
 rootdn "cn=Manager,dc=maxcrc,dc=com"

改成

suffix"dc=micmiu,dc=com"
rootdn "cn=Manager,dc=micmiu,dc=com"

打开控制台,切换到openLDAP安装目录下,启动openLDAP,命令如下:

slapd.exe -d 1 -f ./slapd.conf

会在控制台看到类似如下的日志信息:
在这里插入图片描述
日志信息:slapd starting 表示服务已经启动好了。

新建一个文件:C:\OpenLDAP\mydemo.ldif ,内容如下:

dn:dc=micmiu,dc=com
objectclass:domain
objectclass:top
o:Michael Blog
dc:micmiu
 
dn:ou=Developer,dc=micmiu,dc=com
objectclass:organizationalUnit
ou:Developer
description:Container fordeveloper entries
 
dn:ou=Tester,dc=micmiu,dc=com
objectclass:organizationalUnit
ou:Tester
description:Container fortest entries
 
dn:uid=Michael,ou=Developer,dc=micmiu,dc=com
uid:Michael
objectClass:inetOrgPerson
mail:sjsky_007@gmail.com
userPassword:111111
labeledURI:http://www.micmiu.com
sn:Sun
cn:Michael Sun
 
dn:uid=Miumiu,ou=Tester,dc=micmiu,dc=com
uid:Miumiu
objectClass:inetOrgPerson
userPassword:111111
labeledURI:http://www.micmiu.com
sn:Wu
cn:Miumiu Wu

tips:格式要严格,每行的开头和末尾不能有空格

然后在控制台中切换到openLDAP安装目录下执行ldapadd命令:

slapadd -v -l mydemo.ldif -f slapd.conf

参数说明:

-x 使用简单验证方式
-D 指定管理员DN(与slapd.conf中配置的一致)
-W 大写W表示回车后根据提示输入密码,可以使用小写的-w password 直接输入密码
-f 需要导入数据LDIF的文件名
-h 目录服务器的IP地址
mydemo.ldif是文件的名称

添加成功会显示如下信息:
在这里插入图片描述
验证添加的信息,

先进入到OpenLDAP的ClientTools文件中
cd C:\OpenLDAP\ClientTools
在执行
ldapsearch-x-b"dc=micmiu,dc=com" "(objectclass=*)"

查询结果如下:
在这里插入图片描述
验证成功。

[五]、客户端介绍

网上搜索到一个客户端:LdapBrowser282 附件提供相关下载:http://www.micmiu.com/wp-content/uploads/2012/05/LdapBrowser282.zip

下载解压后直接双击:lbe.bat 文件即可运行。
在这里插入图片描述
选择localhost进入
在这里插入图片描述
可以正确查询到之前添加的相关信息。
在这里插入图片描述
[六]、Java调用接口

package com.doaron.controller;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;
import java.util.Hashtable;
import java.util.Vector;


public class TestOpenLDAP {
    DirContext dc = null;
    String account = "Manager";//操作LDAP的帐户。默认就是Manager。
    String password = "secret";//帐户Manager的密码。
    String root = "dc=micmiu,dc=com"; //LDAP的根节点的DC

  /*  public TestOpenLDAP() {
        //init();

        //添加节点
        //add();

        //delete("ou=hi,dc=example,dc=com");//删除"ou=hi,dc=example,dc=com"节点

        //modifyInformation("ou=hi,dc=example,dc=com");//修改"ou=hi,dc=example,dc=com"属性


        searchInformation("dc=example,dc=com", "", "(objectclass=*)");//遍历所有根节点

        //重命名节点"ou=new,o=neworganization,dc=example,dc=com"
        //renameEntry("ou=new,o=neworganization,dc=example,dc=com","ou=neworganizationalUnit,o=neworganization,dc=example,dc=com");

        //searchInformation("o=neworganization,dc=example,dc=com","","(objectclass=*)");//遍历指定节点的分节点
        close();
    }*/

    public  DirContext init() {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://127.0.0.1:389/");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root);
        env.put(Context.SECURITY_CREDENTIALS, password);
        try {
            dc = new InitialDirContext(env);//初始化上下文
            System.out.println("认证成功");//这里可以改成异常抛出。
        } catch (javax.naming.AuthenticationException e) {
            System.out.println("认证失败");
        } catch (Exception e) {
            System.out.println("认证出错:" + e);
        }
        return null;
    }

    public void close() {
        if (dc != null) {
            try {
                dc.close();
            } catch (NamingException e) {
                System.out.println("NamingException in close():" + e);
            }
        }
    }

    public void add() {
        try {
            //组织单位,倒数第二个根节点
            /*String newUserName = "hi1";
            BasicAttributes attrs = new BasicAttributes();
            BasicAttribute objclassSet = new BasicAttribute("objectClass");
            objclassSet.add("top");
            objclassSet.add("organizationalUnit");
            attrs.put(objclassSet);
            attrs.put("ou", newUserName);
            dc.createSubcontext("ou=" + newUserName + "," + root, attrs);*/


            //组织人员
            String newUserName = "hi2";
            BasicAttributes attrs = new BasicAttributes();
            BasicAttribute objclassSet = new BasicAttribute("objectClass");
            objclassSet.add("inetOrgPerson");
            attrs.put(objclassSet);
            attrs.put("ou", newUserName);
            attrs.put("uid","Tester1");
            attrs.put("sn","用户名称");
            attrs.put("cn","账号名称");
            attrs.put("mail","1575687@163.com");
            attrs.put("mobile","55758849");
            attrs.put("userPassword","234234");
            dc.createSubcontext("uid=hi2,ou=Demo,dc=app1,dc=micmiu,dc=com", attrs);


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception in add():" + e);
        }
    }

    public void delete(String dn) {
        try {
            dc.destroySubcontext(dn);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception in delete():" + e);
        }
    }

    public boolean modifyInformation(String dn) {
        try {
            ModificationItem[] mods = new ModificationItem[1];

            /*添加属性*/
//            Attribute attr0 = new BasicAttribute("sn",
//                    "测试");
//            mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,attr0);

            /*修改属性*/
            Attribute attr0 = new BasicAttribute("sn", "测试1");
            mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    attr0);

            /*删除属性*/
//            Attribute attr0 = new BasicAttribute("sn",
//                    "Tester1");
//            mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
//                    attr0);
            dc.modifyAttributes(dn, mods);
            return true;
        } catch (NamingException ne) {
            ne.printStackTrace();
            System.err.println("Error: " + ne.getMessage());
            return false;
        }

    }

    /**
     * @param base :根节点(在这里是"dc=example,dc=com")
     * @param scope :搜索范围,分为"base"(本节点),"one"(单层),""(遍历)
     * @param filter :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点)
     */
    public void searchInformation(String base, String scope, String filter) {
        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);
            // Use the NamingEnumeration object to cycle through
            // the result set.
            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();
                    // Another NamingEnumeration object, this time
                    // to iterate through attribute values.
                    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 boolean renameEntry(String oldDN, String newDN) {
        try {
            dc.rename(oldDN, newDN);
            return true;
        } catch (NamingException ne) {
            System.err.println("Error: " + ne.getMessage());
            return false;
        }
    }

    public static void main(String[] args) {
        TestOpenLDAP testOpenLDAP=new TestOpenLDAP();
        //认证
        DirContext ctx = testOpenLDAP.init();

        //遍历所有根节点
        testOpenLDAP.searchInformation("uid=hi2,ou=Demo,dc=app1,dc=micmiu,dc=com","","(objectclass=*)");


        //添加
        //testOpenLDAP.add();


        //修改属性"ou=hi1,dc=micmiu,dc=com"属性
         //testOpenLDAP.modifyInformation("uid=hi2,ou=Demo,dc=app1,dc=micmiu,dc=com");

        //删除"ou=hi1,dc=micmiu,dc=com"节点
        // testOpenLDAP.delete("ou=hi1,"+ testOpenLDAP.root);



        //关闭
        testOpenLDAP.close();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不秃头的程序员.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值