【发布时间】:2020-03-31 07:54:56
【问题描述】:
我想连接到 AD 并按用户过滤以检查它是否存在 (uid)。以后我也会检查密码的。
目前,我尝试实现一些简单的操作,例如在屏幕上列出所有用户、他们的姓氏和他们的 id,但它没有奏效。目标是获取包含所有 uid 的列表,并检查我的网站数据库中是否存在相同的 uid。
我收到此错误:LDAP:错误代码 32 – 没有这样的对象
这是我第一次使用 AD,我认为我在 AD 树上做错了。
这是我尝试过的代码:
public class ActiveDirectory {
private Properties properties;
private DirContext dirContext;
private boolean conected = false;
private String Error;
public ActiveDirectory(String username, String password, String domainController) {
//Path keystore whith the registred SSL certficate
String keystorePath = "C:\\Program Files\\Java\\jdk-12.0.2\\lib\\security\\cacerts";
System.setProperty("javax.net.ssl.keyStore", keystorePath);
System.setProperty("javax.net.ssl.keyStorePassword", "******");
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.SECURITY_AUTHENTICATION,"simple");
properties.put(Context.PROVIDER_URL, "LDAPS://kldap.***.***:636");
properties.put(Context.SECURITY_PRINCIPAL, "uid=blabla,ou=blabla,DC=blabla,DC=blabla");
properties.put(Context.SECURITY_CREDENTIALS, "******");
//Initializing active directory LDAP connection
try {
dirContext = new InitialDirContext(properties);
String searchFilter = "(objectClass=inetOrgPerson)";
String[] requiredAttributes= {"sn", "cn", "employeeNumber"};
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(requiredAttributes);
NamingEnumeration users = dirContext.search("ou=Users,o=Company", searchFilter, controls);
SearchResult searchResult = null;
String commonName = null;
String surName = null;
String employeeNum = null;
while (Users.hasMore()) {
searchResult = (SearchResult) Users.next();
Attributes attr = searchResult.getAttributes();
commonName = attr.get("cn").get(0).toString();
surName = attr.get("sn").get(0).toString();
employeeNum = attr.get("employeeNumber").get(0).toString();
System.out.println("Name: " + commonName);
System.out.println("Surname: " + surName);
System.out.println("Employee number = " + employeeNum);
}
conected = true;
} catch (NamingException e) {
conected = false;
Error = e.getMessage();
LOG.severe(e.getMessage());
e.printStackTrace();
}
}
public boolean isConected() {
if (conected) {
return true;
}else {
return false;
}
}
public String getError() {
return Error;
}
}
【问题讨论】:
标签: java active-directory ldap jndi