List a user's groups on an AD server

0

I need to list the groups of a user X on the AD server. I have the following code snippet 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 do not know much about it. Can someone help me with this filter?

    
asked by anonymous 25.01.2017 / 12:38

1 answer

0

After much searching and testing, I was able to solve:

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);
        props.put(Context.SECURITY_CREDENTIALS, PASSWORD);
        DirContext context;
        context = new InitialDirContext(props);
        String usersContainer = "cn=Users,dc=teste,dc=local";
        SearchControls ctls = new SearchControls();
        String[] attrIDs = { "memberOf" };
        ctls.setReturningAttributes(attrIDs);
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration<?> answer = context.search(usersContainer, "(&(objectClass=user)(sAMAccountName=dteste))", ctls);
        while (answer.hasMore()) {
            SearchResult rslt = (SearchResult) answer.next();
            Attributes attrs = rslt.getAttributes();
            String resultado = attrs.toString();
            resultado = resultado.substring(20,resultado.length()-1);
            getGrupos(resultado);
        }

        context.close();

        return true;
    }

    public static void getGrupos(String resultado){
        String[] partes = resultado.split(", ");

        for (String parte : partes){
            parte = parte.trim();
            parte = parte.substring(3, parte.indexOf(",CN"));
            System.out.println(parte);
        }
    }
    
25.01.2017 / 19:05