Java 中的 LDAP 如何针对此 LDAP 进行搜索/身份验证
我正在使用 LDAP 和 Java 搜索。 这是我的 LDIF 导出,具有简单的组织结构
version: 1
dn: dc=example,dc=com
objectClass: organization
objectClass: dcObject
objectClass: top
dc: example
o: MyOrganization
description: Test Description
dn: ou=people, dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
ou: people
description: All users in demo company
dn: cn=Johnny Doe,ou=people,dc=example,dc=com
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top
cn: Johnny Doe
sn: Johnny
homephone: 123-456-7890
mail: [email protected]
ou: Development
uid: jjohnny
userpassword:: johnny
dn: cn=Samuel Johnson,ou=people,dc=example,dc=com
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top
cn: Samuel Johnson
sn: Samuel
homephone: 123-456-7890
mail: [email protected]
ou: Accounts
uid: ssam
userpassword:: sammy
如何运行 Java 代码片段以从 LDAP 服务器获取所有用户?我的 Apache DS Directory Server 上没有身份验证设置。
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/dc=example,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "none");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
Object obj = new Object();
// want to print all users from the LDAP server
System.out.println(obj.toString());
ctx.close();
}
I am playing with LDAP and Java search.
Here's my LDIF export with a simple organization
version: 1
dn: dc=example,dc=com
objectClass: organization
objectClass: dcObject
objectClass: top
dc: example
o: MyOrganization
description: Test Description
dn: ou=people, dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
ou: people
description: All users in demo company
dn: cn=Johnny Doe,ou=people,dc=example,dc=com
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top
cn: Johnny Doe
sn: Johnny
homephone: 123-456-7890
mail: [email protected]
ou: Development
uid: jjohnny
userpassword:: johnny
dn: cn=Samuel Johnson,ou=people,dc=example,dc=com
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top
cn: Samuel Johnson
sn: Samuel
homephone: 123-456-7890
mail: [email protected]
ou: Accounts
uid: ssam
userpassword:: sammy
How do I run a Java snippet to get all users from the LDAP server? There's no authentication set-up on my Apache DS Directory Server.
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/dc=example,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "none");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
Object obj = new Object();
// want to print all users from the LDAP server
System.out.println(obj.toString());
ctx.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种方法是使用 UnboundID。它的 API 非常易读且简短
创建 Ldap 连接
获取过滤结果
获取所有组织单位和容器
获取特定组织单位< /strong>
获取组织部门下的所有用户
获取组织部门下的特定用户
显示结果
Another approach is using UnboundID. Its api is very readable and shorter
Create a Ldap Connection
Get filter result
Get all Oragnization Units and Containers
Get a specific Organization Unit
Get all users under an Organizational Unit
Get a specific user under an Organization Unit
Display result
您还可以使用以下代码:
You can also use the following code :