gpt4 book ai didi

java - 在 java 中使用 Ldap 获取组

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:37 25 4
gpt4 key购买 nike

如何使用 Active Directory 获取特定用户组?我正在获取所有组,但我想获取用户所属的组

  public static String ldapUri = "ldap://pdc.example.com:389";
public static String usersContainer = "cn=users,dc=example,dc=com";
public ArrayList<String> getUserGroups(String email, String password){
ArrayList<String> list = new ArrayList<String>();
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUri);
env.put(Context.SECURITY_PRINCIPAL, email);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
DirContext ctx = new InitialDirContext(env);
SearchControls ctls = new SearchControls();
String[] attrIDs = { "cn" };
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);

NamingEnumeration answer = ctx.search(usersContainer, " (objectclass=group)", ctls);
while (answer.hasMore()) {
SearchResult rslt = (SearchResult) answer.next();
Attributes attrs = rslt.getAttributes();
String groups = attrs.get("cn").toString();
String [] groupname = groups.split(":");
String userGroup = groupname[1];
System.out.println(attrs.get("cn"));
}
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
return list;
}

最佳答案

我正在使用这个获取用户的所有组

String[] attrIDs = {"cn"};
ctls.setReturningAttributes(attrIDs);
String[] attributes = {"memberOf"};
ctls.setReturningAttributes(attributes);
NamingEnumeration<?> answer = ctx.search(usersContainer, "(&(objectclass=user)(sAMAccountName=userName))", ctls);

关于java - 在 java 中使用 Ldap 获取组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34491680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com