使用 Java 获取 LDAP 域用户名列表
LDAP 用户名需要作为自动完成功能显示在输入框中。我正在尝试获取用户列表,如下所示:
String ldapURL = "ldap://192.26.75.5:389/dc=northamerica,dc=company,dc=com";
String principalPrefix = "domainName";
String username = SecurityContextHolder.getContext().getAuthentication().getName();
String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
Hashtable<String, String>environment = new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL,ldapURL);
environment.put(Context.SECURITY_AUTHENTICATION,"simple");
environment.put(Context.SECURITY_PRINCIPAL,principalPrefix + "\\" + username);
environment.put(Context.SECURITY_CREDENTIALS,password);
environment.put( Context.REFERRAL, "follow" );
DirContext context = null;
NamingEnumeration<SearchResult> enumResult = null;
try
{
context = new InitialDirContext(environment);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs ={"ou","uid", "givenname", "sn", "mail"};
controls.setReturningAttributes(attrIDs);
enumResult = context.search("","(&(objectCategory=person)(objectClass=user)(CN=*))", controls);
if(enumResult != null)
{
//authentication successful
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
但是“enumResult”始终获取单个用户值。如果我遗漏了什么或者这样做的方法是否错误,请告诉我。 任何帮助/建议/建议将不胜感激!谢谢。
The ldap user names need to be displayed in the the input box as autocomplete feature. I am trying to get list of users as below:
String ldapURL = "ldap://192.26.75.5:389/dc=northamerica,dc=company,dc=com";
String principalPrefix = "domainName";
String username = SecurityContextHolder.getContext().getAuthentication().getName();
String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
Hashtable<String, String>environment = new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL,ldapURL);
environment.put(Context.SECURITY_AUTHENTICATION,"simple");
environment.put(Context.SECURITY_PRINCIPAL,principalPrefix + "\\" + username);
environment.put(Context.SECURITY_CREDENTIALS,password);
environment.put( Context.REFERRAL, "follow" );
DirContext context = null;
NamingEnumeration<SearchResult> enumResult = null;
try
{
context = new InitialDirContext(environment);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs ={"ou","uid", "givenname", "sn", "mail"};
controls.setReturningAttributes(attrIDs);
enumResult = context.search("","(&(objectCategory=person)(objectClass=user)(CN=*))", controls);
if(enumResult != null)
{
//authentication successful
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
However "enumResult" always gets single user value. Let me know if i am missing out something or if its the wrong way to do it.
Any help/advice/suggestion would be appreciated !! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论