Java中的SearchResult [英] SearchResult in Java

查看:850
本文介绍了Java中的SearchResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码列出了LDAP中的用户:

I list users from LDAP with the following code:

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT);
    env.put(Context.PROVIDER_URL, HOST); 
    env.put(Context.SECURITY_PRINCIPAL,USER); 
    env.put(Context.SECURITY_CREDENTIALS,PASSWORD);


    DirContext ctx = new InitialDirContext(env);

    SearchControls sc = new SearchControls();
    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration items = ctx.search(BASE, filter, sc);
    while (items != null && items.hasMore()) {
        SearchResult sr = (SearchResult)items.next();
        System.out.println("SR : " + sr) ;
    }

现在我得到的输出是

SR : cn=smith: null:null:{objectclass=objectClass: person, sn=sn: smith, cn=cn: smith}
SR : cn=king: null:null:{objectclass=objectClass: person, sn=sn: king, cn=cn: king}

如何从SearchControls获得仅输出,如:

How can I get from SearchControls just the output like:

sn = smith |  cn = smith
sn = king  |  cn = king

推荐答案

对您的sysout进行如下简单更改是否足够?

Wouldn't a simple change in your sysout as below suffice?

System.out.println("SR : " 
+ sr.getAttributes().get("sn")
+ " | " 
+ sr.getAttributes().get("cn")
) ;

或者,我在这里阅读不正确吗?

Or, am I reading something incorrectly here?

这篇关于Java中的SearchResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆