【发布时间】:2015-06-30 18:38:23
【问题描述】:
我想查询 Active Directory 用户属性,包括自定义属性。我收到的内置属性很好,但Attributes 对象中的自定义属性为空。我知道它在那里并且已经设置好了,因为我得到了在 PowerShell 程序中工作的等价物。
这是我正在做的事情:
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = { "distinguishedName", "mail", "mycustomattribute"};
constraints.setReturningAttributes(attrIDs);
NamingEnumeration<SearchResult> answer = ldapContext.search("DC=mydomain,DC=lan", "sAMAccountName="
+ user, constraints);
if (answer.hasMore())
{
Attributes attrs = answer.next().getAttributes();
System.out.println("distinguishedName "+ attrs.get("distinguishedName"));
System.out.println("mail "+ attrs.get("mail"));
System.out.println("custom "+ attrs.get("mycustomattribute"));
}
这会正确输出专有名称和邮件,但它会为自定义属性输出 null。
我首先使用 PowerShell 进行了测试,以确保属性设置正确。我做了一个$user = Get-ADUser ...,然后输出$user.mycustomattribute,得到了预期值。我的 Java 应用程序需要这个值。
到目前为止,这是我的思考过程......
是否需要以不同方式访问自定义属性?我不确定,但 Google 查询并没有显示任何建议。
这可能与查询的域控制器没有该数据有关吗?在设置 LdapContext 对象时,我确实有 hashtable.put(Context.REFERRAL, "follow"); 用于环境属性,所以我猜这排除了这一点。
我刚刚意识到,如果我使用 ADSI Edit(浏览数据的 MS 工具),如果我以非域管理员用户身份运行自定义属性,则自定义属性显示为 <not set>,但如果我以域管理员,它在 ADSI Edit 中正确显示属性。我以为我已经弄明白了,所以我以域管理员的身份运行 Java 应用程序,但它仍然无法工作(获取其他属性,但没有自定义属性)。
【问题讨论】:
标签: java active-directory jndi