使用JAVA获取OpenLDAP中的用户组

问题描述 投票:-1回答:2

我不太了解java,但我需要更改代码。这里的背景是我们在LDAP中使用代码,它将为我们分配给已登录用户的组。现在,由于某种原因,我们必须切换到OpenLDAP,这里出现了问题。在这里,我们无法获得分配给用户的组。

以前我用来获得这些团体

上下文名称在这里ou = People,dc = maxcrc,dc = com

NamingEnumeration<SearchResult> search 
    = context.search(contextName, 
            "(sAMAccountName=" + userId + ")", constraints);

现在,我尝试了各种组合

NamingEnumeration<SearchResult> search 
          = context.search(contextName, 
                   "(uid=" + userId + ")",  constraints);

NamingEnumeration<SearchResult> search 
        = context.search(contextName, 
                "(&(objectClass=groupOfNames)(cn=+userId)",  constraints);

和别的。

问题在这里我没有得到组名。那么,我是如何寻找群体或我没有得到的。有谁可以帮助我。

这是我们的代码

public static HashMap getGroupList(
        DirContext context, String userId, String key) 
        throws NamingException, NullArgumentException, 
                InvalidStringValueException, ParserException {

  //setting sonstraints ans searach control to subtree scope
  HashMap groupList = new HashMap();
  SearchControls constraints = new SearchControls();
  constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
  constraints.setReturningAttributes(new String[]{"cn", MEMBER_OF_ATTRIBUTE});

  String contextName = parser.getConfigNodeValue("contextName");
  logger.debug("Context Name: " + contextName);
  logger.debug("Finding Group List for user ID: " + userId);
  NamingEnumeration<SearchResult> search 
          = context.search(contextName, 
                  SAMAC_COUNT_NAME + userId + CLOSE_BRACKET, constraints);

  //searching attribute
  logger.debug("searching attribute");

  SearchResult searchResult = null;

  String value = "";
  while (search.hasMoreElements()) {
      searchResult = search.next();
      String groupName = searchResult.getAttributes().get(MEMBER_OF_ATTRIBUTE).toString();
      groupList.put(groupName, groupName);
  }

  return groupList;
}

编辑:

这里的上下文名称是ou=People,dc=maxcrc,dc=com,我已经将各种搜索过滤器应用为(uid=userId)(&(objectClass=groupOfNames)(uid=userId))(&(objectClass=user)(uid=userId)),但我什么也没得到。我需要知道如何在这里搜索。

目录很简单 -

在dc = maxcrc dc = com中有ou = People,并且该演示中有一个用户,并且演示有一个组的一部分。对象类是用户的inetOrgPerson

java ldap openldap
2个回答
0
投票

输出什么都没有

这并不意味着属性是空的。如果是这样的话,你会看到logger.debug(groupName + " group name found for " + userId);的输出。你没有,很明显搜索本身没有返回任何内容,即你的过滤器或启动DN有问题。

编辑重新编辑,只有第一个过滤器才有意义。第二个是语法错误,第三个是搜索组而不是用户,而且用户将拥有memberOf属性,而不是组。这里仍然没有足够的信息进一步评论。

编辑2

上下文名称是ou=People,dc=maxcrc,dc=com

好。

我已经将各种搜索过滤器应用为(uid=userId)

你的意思是(uid={0})的参数值为userId?你应该。 userId的价值是什么?

这也是(&(objectClass=groupOfNames)(uid=userId))

这只是胡说八道:

  1. ou=People下不会有(不应该)小组;
  2. 组对象不具有memberOf属性。用户将拥有memberOf属性,显示他们所属的组。在群体内部寻找它是没有任何意义的。

这也是(&(objectClass=user)(uid=userId))

往上看。这要求用户对象具有objectClassuser。他们呢?如果没有,他们有什么,为什么你不使用它?

请回答关于目录树的相关部分是什么样的问题。包括对象类。


-1
投票

我一直都错了。我们的OpenLDAP中没有memberof属性,因此代码不起作用。

所以我需要稍微更改代码,以便我对用户进行身份验证,然后我应该查询每个存在的组,并检查这些组中是否存在该用户名。

所以,即使没有我可以解决的成员。

这是我使用的示例代码 -

import javax.naming.NamingException;


public class LdapQuery {
    public static void main(String[] args) throws NamingException {
        SimpleLdapAuthentication obj = new SimpleLdapAuthentication();
        obj.ldapquery();
    }

}

这是方法

import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class SimpleLdapAuthentication {

    public String ldapquery() {
        String distName = "";
        String username = "cn=demo,ou=People,dc=saas,dc=com";
        String[] userID = new String[2];
        userID[0] = "Users";
        userID[1] = "Developers";
        int size = userID.length;
        String password = "sacs3";
        String groupName = "";
        String base = "ou=People,dc=maxcrc,dc=com";
        //String searchFilter = "cn=" + username + "," + base;
        String ldapURL = "ldap://yourldapurl";
        Hashtable<String, String> environment = new Hashtable<String, String>();
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        environment.put(Context.PROVIDER_URL, ldapURL);
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, username);
        environment.put(Context.SECURITY_CREDENTIALS, password);
        String[] returnAttribute = {"member"};
        SearchControls srchControls = new SearchControls();
        srchControls.setReturningAttributes(returnAttribute);
        srchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        for (int i = 0; i <= size - 1; i++) {
            String searchFilter = "(cn=" + userID[i] + ")";
            try {
                DirContext authContext = new InitialDirContext(environment);
                //System.out.println("Authentication Successful");
                NamingEnumeration<SearchResult> search = authContext.search(base, searchFilter, srchControls);
                // Probably want to test for nulls here
                distName = search.nextElement().toString();

                String[] splitBasedOnColon = distName.split("\\:");
                for (String x : splitBasedOnColon) {
                    if (x.startsWith("cn")) {
                        String[] splitGroupName = x.split("\\=");
                        groupName = splitGroupName[1];
                    }
                }
                if (distName.contains(username)) {
                    System.out.println("User is part of the group: " + groupName);
                }
            } catch (AuthenticationException authEx) {
                System.out.println("Authentication failed!");
            } catch (NamingException namEx) {
                System.out.println("Something went wrong!");
                namEx.printStackTrace();
            } catch (NullPointerException notFound) {
                System.out.println("User is not part group : "+ userID[i]);
               // notFound.printStackTrace();
            }
        }
        return distName;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.