LDAP サーバーを使用して Active Directory にクエリを実行し、Active Directory から定義済みの ACTIVE ユーザーのリストを取得できる必要があります。
LDAPサーバーへの接続に成功して、これを実行しようとしました。以下の Java コードでは、accountExpires 属性を使用すると 1 つのレコードのみが返されます。各レコードが DISPLAY NAME および MAIL 属性を表示するレコードのリストを LDAP サーバーから取得する必要があります。
これが私のコードです:
public static void main(String[] args) {
ADUserAttributes adUserAttributes = new ADUserAttributes();
adUserAttributes.getLdapContext());
adUserAttributes.getActiveEmpRecordsList("0", adUserAttributes.getLdapContext());
}
public LdapContext getLdapContext(){
LdapContext ctx = null;
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "Simple");
env.put(Context.SECURITY_PRINCIPAL, "e~inventory");
env.put(Context.SECURITY_CREDENTIALS, "password");
env.put(Context.PROVIDER_URL, "ldap://xxxdc01.txh.org");
ctx = new InitialLdapContext(env, null);
System.out.println("Connection Successful.");
} catch(NamingException nex){
System.out.println("LDAP Connection: FAILED");
nex.printStackTrace();
}
return ctx;
}
private List<String> getActiveEmpRecordsList(String accountExpires, LdapContext ctx) {
List<String> activeEmpAttributes = new ArrayList<String>();
Attributes attrs = null;
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = {"displayname", "mail"};
constraints.setReturningAttributes(attrIDs);
NamingEnumeration answer = ctx.search("DC=txh,DC=org", "accountExpires=" + accountExpires, constraints);
if (answer.hasMore()) {
attrs = ((SearchResult) answer.next()).getAttributes();
int empNameLen = attrs.get("displayname").toString().length();
int empEmailAddrLen = attrs.get("mail").toString().length();
activeEmpAttributes.add(attrs.get("displayname").toString().substring(13, empNameLen));
activeEmpAttributes.add(attrs.get("mail").toString().substring(6, empEmailAddrLen));
ctx.close();
}else{
throw new Exception("Invalid User");
}
System.out.println("activeEmpAttributes: " + activeEmpAttributes);
System.out.println("count: " + activeEmpAttributes.size());
} catch (Exception ex) {
ex.printStackTrace();
}
return activeEmpAttributes;
}