LDAP安装及基本操作

LDAP安装

特别说明:以下都是针对mac环境,mac OS版本是10.15.7。

LDAP的安装方式有两种。第一种是直接使用ApacheDirectory,您可以从官网下载。 (安装和启动服务器应少于5分钟)

下载下来的是压缩包,将其解压后并移动到/usr/local/目录下,然后进入到/usr/local/apacheds-2.0.0.AM26/bin中,使用命令./apacheds.sh start即可启动。

第二种方式是使用Apache Directory Studio。
官网下载地址:Apache Directory Studio,下载下来之后是dmg的可运行文件。

在左下角的右侧面板,可以看到 LDAP Server可以看到这里也可以启动一个ApacheDS。
在这里插入图片描述

LDAP建立用户

参考学习的视频是:bilibili-小巧好用的ldap服务器: Apache Directory Studio

对LDAP中建立的用户,可以使用下面的命令方式验证建立的用户是否正确。

在linux系统中,可以下载安装openldap软件,然后就可以使用下面的命令了。由于我使用的是mac系统,默认是支持了ldapsearch命令。但是我使用下面这种方式验证,一直会报错。

具体报错是:

ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)

但是,我使用java代码的形式,是验证通过了的。暂时不清楚为啥下面的命令执行会报错。

ldapsearch -h localhost -p 10389 -D "uid=zhangsan,ou=users,dc=example,dc=com" -w 你设置的用户![在这里插入图片描述](https://img-blog.csdnimg.cn/118f73db07684afabaa4a85a21c96a1c.png)
密码 -x b "ou=users,dc=example,dc=com"

如果正确通过,会展示用户的信息。如果密码输入错误,也会提示认证失败。
在这里插入图片描述

参考网址:
1、https://blog.csdn.net/dnc8371/article/details/106703325

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LDAP(Lightweight Directory Access Protocol)是一种轻量级目录访问协议,旨在提供对目录服务的快速访问。在Java中,可以通过使用JNDI(Java Naming and Directory Interface)API来操作LDAP。 下面是一些Java操作LDAP的常见步骤和示例代码: 1.创建连接对象 ``` 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://localhost:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com"); env.put(Context.SECURITY_CREDENTIALS, "password"); DirContext ctx = new InitialDirContext(env); ``` 2.查询数据 ``` String base = "ou=people,dc=example,dc=com"; String filter = "(objectclass=person)"; SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<SearchResult> results = ctx.search(base, filter, sc); while (results.hasMore()) { SearchResult sr = (SearchResult) results.next(); // 处理查询结果 } ``` 3.添加数据 ``` Attributes attrs = new BasicAttributes(); attrs.put("cn", "John Smith"); attrs.put("sn", "Smith"); attrs.put("givenName", "John"); attrs.put("mail", "john.smith@example.com"); attrs.put("userPassword", "password"); ctx.createSubcontext("cn=John Smith,ou=people,dc=example,dc=com", attrs); ``` 4.修改数据 ``` ModificationItem[] mods = new ModificationItem[1]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", "new-email@example.com")); ctx.modifyAttributes("cn=John Smith,ou=people,dc=example,dc=com", mods); ``` 5.删除数据 ``` ctx.destroySubcontext("cn=John Smith,ou=people,dc=example,dc=com"); ``` 这些是Java操作LDAP的一些基本步骤和示例代码,但实际应用中还需要考虑LDAP服务器的具体配置和授权等问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值