使用 JAVA 在 OpenLDAP 中获取用户组
Get user groups in OpenLDAP with JAVA
我不太了解 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
该演示中有一个用户,该演示是一个组的一部分。对象 class 是用户
的 inetOrgPerson
the output was nothing
这并不意味着该属性为空。如果是这种情况,您将看到 logger.debug(groupName + " group name found for " + userId);
的输出。正如您没有做的那样,显然搜索本身没有 return 任何东西,即您的过滤器或启动 DN 有问题。
EDIT 重新编辑,只有第一个过滤器有意义。第二个是语法错误,第三个搜索组而不是用户,具有 memberOf
属性的是用户,而不是组。这里仍然没有足够的信息来进一步评论。
编辑 2
context name is ou=People,dc=maxcrc,dc=com
好的。
and I have applied the various search filter as (uid=userId)
您是说 (uid={0})
的 参数 的值为 userId
吗?你应该。 userId
的价值是多少?
this also (&(objectClass=groupOfNames)(uid=userId))
这简直是胡说八道:
ou=People
; 下不会有(不应该有)群组
- 组对象将没有
memberOf
属性。 用户 将有一个 memberOf
属性,显示他们所属的组。在组内寻找它毫无意义。
this also (&(objectClass=user)(uid=userId))
见上文。这要求用户对象具有 user
的 objectClass
。他们呢?如果没有,他们有什么 do,你为什么不使用它?
并且请回答有关目录树的相关部分是什么样子的问题。包括对象 类.
我一直都错了。我们的 OpenLDAP 中没有 memberof 属性,因此代码将无法运行。
所以我需要稍微更改一下代码,以便对用户进行身份验证,然后我应该查询存在的每个组并检查这些组中是否存在该用户名。
所以,即使没有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;
}
}
我不太了解 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 该演示中有一个用户,该演示是一个组的一部分。对象 class 是用户
的 inetOrgPersonthe output was nothing
这并不意味着该属性为空。如果是这种情况,您将看到 logger.debug(groupName + " group name found for " + userId);
的输出。正如您没有做的那样,显然搜索本身没有 return 任何东西,即您的过滤器或启动 DN 有问题。
EDIT 重新编辑,只有第一个过滤器有意义。第二个是语法错误,第三个搜索组而不是用户,具有 memberOf
属性的是用户,而不是组。这里仍然没有足够的信息来进一步评论。
编辑 2
context name is
ou=People,dc=maxcrc,dc=com
好的。
and I have applied the various search filter as
(uid=userId)
您是说 (uid={0})
的 参数 的值为 userId
吗?你应该。 userId
的价值是多少?
this also
(&(objectClass=groupOfNames)(uid=userId))
这简直是胡说八道:
ou=People
; 下不会有(不应该有)群组
- 组对象将没有
memberOf
属性。 用户 将有一个memberOf
属性,显示他们所属的组。在组内寻找它毫无意义。
this also
(&(objectClass=user)(uid=userId))
见上文。这要求用户对象具有 user
的 objectClass
。他们呢?如果没有,他们有什么 do,你为什么不使用它?
并且请回答有关目录树的相关部分是什么样子的问题。包括对象 类.
我一直都错了。我们的 OpenLDAP 中没有 memberof 属性,因此代码将无法运行。
所以我需要稍微更改一下代码,以便对用户进行身份验证,然后我应该查询存在的每个组并检查这些组中是否存在该用户名。
所以,即使没有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;
}
}