82 lines
3.0 KiB
Plaintext
82 lines
3.0 KiB
Plaintext
package th.co.muangthai.endrprint.util;
|
|
|
|
/**
|
|
* Created by IntelliJ IDEA.
|
|
* User: ZIZU
|
|
* Date: 8/16/12
|
|
* Time: 4:23 PM
|
|
* To change this template use File | Settings | File Templates.
|
|
*/
|
|
|
|
import javax.naming.*;
|
|
import javax.naming.directory.*;
|
|
import java.util.Hashtable;
|
|
|
|
public class MemberOfTest {
|
|
|
|
private static final String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
|
|
private static final String connectionURL = "ldap://10.1.0.5:389";
|
|
private static final String connectionName = "CN=Administrator,CN=Users, DC=muangthai,DC=co,DC=th";
|
|
private static final String connectionPassword = "$t@rPlatt1nuM";
|
|
|
|
// Optioanl
|
|
private static final String authentication = "simple";
|
|
private static final String protocol = null;
|
|
|
|
private static String username = "mtl82764";
|
|
|
|
private static final String MEMBER_OF = "memberOf";
|
|
private static final String[] attrIdsToSearch = new String[] { MEMBER_OF };
|
|
public static final String SEARCH_BY_SAM_ACCOUNT_NAME = "(sAMAccountName=%s)";
|
|
public static final String SEARCH_GROUP_BY_GROUP_CN = "(&(objectCategory=group)(cn={0}))";
|
|
private static String userBase = "DC=muangthai,DC=co,DC=th";
|
|
|
|
public static void main(String[] args) throws NamingException {
|
|
Hashtable<String, String> env = new Hashtable<String, String>();
|
|
|
|
// Configure our directory context environment.
|
|
|
|
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
|
|
env.put(Context.PROVIDER_URL, connectionURL);
|
|
env.put(Context.SECURITY_PRINCIPAL, connectionName);
|
|
env.put(Context.SECURITY_CREDENTIALS, connectionPassword);
|
|
if (authentication != null)
|
|
env.put(Context.SECURITY_AUTHENTICATION, authentication);
|
|
if (protocol != null)
|
|
env.put(Context.SECURITY_PROTOCOL, protocol);
|
|
|
|
InitialDirContext context = new InitialDirContext(env);
|
|
String filter = String.format(SEARCH_BY_SAM_ACCOUNT_NAME, username);
|
|
SearchControls constraints = new SearchControls();
|
|
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
constraints.setReturningAttributes(attrIdsToSearch);
|
|
NamingEnumeration results = context.search(userBase, filter,constraints);
|
|
// Fail if no entries found
|
|
if (results == null || !results.hasMore()) {
|
|
|
|
return;
|
|
}
|
|
|
|
// Get result for the first entry found
|
|
SearchResult result = (SearchResult) results.next();
|
|
|
|
// Get the entry's distinguished name
|
|
NameParser parser = context.getNameParser("");
|
|
Name contextName = parser.parse(context.getNameInNamespace());
|
|
Name baseName = parser.parse(userBase);
|
|
|
|
Name entryName = parser.parse(new CompositeName(result.getName())
|
|
.get(0));
|
|
|
|
// Get the entry's attributes
|
|
Attributes attrs = result.getAttributes();
|
|
Attribute attr = attrs.get(attrIdsToSearch[0]);
|
|
|
|
NamingEnumeration e = attr.getAll();
|
|
|
|
while (e.hasMore()) {
|
|
String value = (String) e.next();
|
|
|
|
}
|
|
}
|
|
} |