How can I access the attributes of this LDAP objec

2019-02-20 17:54发布

I'm hoping someone can help me understand how to work with the object returned by a call to DirContext.lookup.

The following code snippet works and returns an object. I just can't figure out how to get the attributes from the object.

javax.naming.directory.DirContext ctx =
    javax.naming.directory.getContext(false);
Object o = ctx.lookup(rdn); 

Any help would be much appreciated.

标签: java ldap
3条回答
Rolldiameter
2楼-- · 2019-02-20 18:06

Attributes attrs = ctx.getAttributes(dn); will retrieve the user attributes assuming the entry asking for the arrtibute values has proper rights.

However, best practice is you only query for the attributes you need.

If you wish to see all attributes, you should query the objectclass attribute values and then query the schema to obtain "all" the attributes assigned and decide which attributes you need to retrieve.

-jim

查看更多
闹够了就滚
3楼-- · 2019-02-20 18:14

You should know what object you are expecting to receive from the lookup(), explicitly cast to it, and then do whatever you want with it.

In the end you should have something like this:

InitialContext iCtx = new InitialContext();
// load the iCtx with environment variables if necessary
Object o = iCtx.lookup("objectNameOrString");
ExpectedObjectType eot = (ExpectedObjectType) o;
eot.doWhatever();
查看更多
贪生不怕死
4楼-- · 2019-02-20 18:18

In an LDAP directory, you would do:

Attributes attrs = ctx.getAttributes(dn);

To obtain the attributes of the object

查看更多
登录 后发表回答