简体   繁体   English

如何将所有memberOf属性分配给LDAP中的特定用户

[英]How do I get all the memberOf Attribute assign to a particular user in LDAP

I have created an application which uses LDAP for authentication. 我创建了一个使用LDAP进行身份验证的应用程序。 I need to find out all the group name in which the User is assign to. 我需要找出用户分配到的所有组名。 Is there any way to find out that. 有没有办法找出答案。 I have written code but somehow it return only one group name that to randomly. 我编写了代码但不知何故它只返回一个随机的组名。

Below is my code to get all memberOf the users. 下面是我的代码,以获取所有成员的用户。

private class UserAttributesMapper implements AttributesMapper {
        @Override
        public Object mapFromAttributes(Attributes attributes) throws NamingException {
            LdapUser user = new LdapUser();
            user.setCn((String)attributes.get("cn").get());
            user.setMemberOf((String)attributes.get("memberOf").get());

            /*String member = (String)attributes.get("memberOf").get();
            int length = attributes.get("memberOf").size();
            if(member != null){
                for(int i = 0;i<= length; i++){
                    user.setMemberOf(member);
                }
            }*/


            //user.setMemberOf(attributes.get("memberOf").getID());
            user.setsAMAccountName((String)attributes.get("sAMAccountName").get());
            return user;
        }
    }

This class is used to set attribute and return attribute for the user. 该类用于为用户设置属性和返回属性。

Thanks in advanced. 提前致谢。

It was pretty much easier than I thought. 这比我想象的要容易得多。 Please find the code below. 请在下面找到代码。 In this you just need to Enumerate the all the memberOf in for loop and assign int in List, then return the List along with all other attribute. 在这里你只需要枚举for循环中的所有memberOf并在List中赋值int,然后返回List以及所有其他属性。

Below is the code. 下面是代码。

private class UserAttributesMapper implements AttributesMapper {
    @Override
    public Object mapFromAttributes(Attributes attributes) throws NamingException {
        LdapUser user = new LdapUser();
        user.setCn((String)attributes.get("cn").get());
        List<String> memberOf = new ArrayList<String>();

        for(Enumeration vals = attributes.get("memberOf").getAll(); vals.hasMoreElements();){
            memberOf.add((String)vals.nextElement());
        }
        user.setMemberOf(memberOf);
        user.setsAMAccountName((String)attributes.get("sAMAccountName").get());
        user.setMail((String)attributes.get("mail").get());
        return user;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM