java ldap查询,使用Java从LDAP检索所有用户及其角色

I have a Web application. For LDAP I am using Apache Directive Studio.

I want to get all the users and their roles in my application.

I am able to get particular information by using the following code.

import java.util.Properties;

import javax.naming.Context;

import javax.naming.NamingException;

import javax.naming.directory.Attributes;

import javax.naming.directory.DirContext;

import javax.naming.directory.InitialDirContext;

public class DirectorySample {

public DirectorySample() {

}

public void doLookup() {

Properties properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");

properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");

try {

DirContext context = new InitialDirContext(properties);

Attributes attrs = context.getAttributes("dc=example,dc=com");

System.out.println("ALL Data: " + attrs.toString());

} catch (NamingException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

DirectorySample sample = new DirectorySample();

sample.doLookup();

}

}

KPMRk.jpg

I want to show all users and roles list, so i need to change query or something else

Thanks in Advance.

解决方案

You can use org.apache.directory.ldap.client.api.LdapConnection for easy search.

Once you bind the connection, do search on the connection. Loop through the cursor to get the object you want. The first parameter should match your the DN of the users parent. Below example is just to give you an idea.

EntryCursor cursor = connection.search( "ou=users, dc=example, dc=com", "(objectclass=*)", SearchScope.ONELEVEL, "*" );

while ( cursor.next() )

{

Entry entry = cursor.get();

//play with the entry

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值