1
I need to list a user’s groups X on the AD server. I have the following code snippet ready that already authenticates on the server and returns me all groups:
public static boolean authenticateJndi(String username, String password) throws Exception{
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
props.put(Context.PROVIDER_URL, URL);
props.put(Context.SECURITY_AUTHENTICATION, "simple");
props.put(Context.SECURITY_PRINCIPAL, USERNAME);//adminuser - User with special priviledge, dn user
props.put(Context.SECURITY_CREDENTIALS, PASSWORD);//dn user password
DirContext context;
context = new InitialDirContext(props);
String usersContainer = "cn=Users,dc=xxxx,dc=local";
SearchControls ctls = new SearchControls();
String[] attrIDs = { "cn" };
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
@SuppressWarnings("rawtypes")
NamingEnumeration answer = context.search(usersContainer, "(objectclass=group)", ctls);
while (answer.hasMore()) {
SearchResult rslt = (SearchResult) answer.next();
Attributes attrs = rslt.getAttributes();
System.out.println(attrs.get("cn"));
}
context.close();
I don’t have much knowledge on the subject. Can anyone help me in this filter ?