使用Spring LdapTemplate从Active Directory获取所有属性 [英] Get all attributes from Active Directory using Spring LdapTemplate

查看:183
本文介绍了使用Spring LdapTemplate从Active Directory获取所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用LDAP验证用户身份的Spring Boot应用程序.对于用户,我正在映射AD中的属性,并填充诸如用户的名字,姓氏,部门,电子邮件,电话以及图像之类的值.但是,我无法从属性中获取员工编号.当我使用工具 Active Directory资源管理器检查属性时,我是能够看到每个条目88个属性.但是,当我使用此代码从上下文中打印每个属性时,

I have a Spring Boot application that uses LDAP to authenticate the users. For the users, I am mapping the attributes from AD and populating the values like the user's first name, last name, department, email, telephone, and also the image. However, I am unable to get the employee number from the attributes. When I check the attributes using the tool Active Directory explorer, I am able to see 88 attributes per entry. However, when I print every attribute from the context using this code,

@Bean
public UserDetailsContextMapper userDetailsContextMapper() {
    return new LdapUserDetailsMapper() {
        @Override
        public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {

            String email = ctx.getStringAttribute("mail");
            String department = ctx.getStringAttribute("department");
            String empNumber = ctx.getStringAttribute("employeeNumber");
            System.out.println(empNumber); // this prints null

            System.out.println(ctx.attributeExists("employeeNumber")); // this prints false



            byte[] value= (byte[])ctx.getObjectAttribute("thumbNailPhoto");
            BASE64Encoder base64Encoder = new BASE64Encoder();
            StringBuilder imageString = new StringBuilder();
            imageString.append("data:image/jpg;base64,");
            imageString.append(base64Encoder.encode(value));
            String image = imageString.toString();

            Attributes attributes = ctx.getAttributes();

            NamingEnumeration<? extends Attribute> namingEnumeration = attributes.getAll();

            try {
                while(namingEnumeration.hasMore()){ 
                 /*this loop prints 75 attributes but employeeNumber attribute is missing along with some other attributes*/
                    Attribute attribute = namingEnumeration.next();
                    System.out.println(attribute); 
                }
            } catch (NamingException e) {
                e.printStackTrace();
            }

            CustomUserDetails userDetails = (CustomUserDetails)userService.loadUserByUsername(username);
            userDetails.setImage(image);
            userDetails.setEmail(email);
            userDetails.setDepartment(department);

            return userDetails;
        }
    };
}

仅打印75个属性.为什么某些属性没有被检索?我该如何访问这些属性?

only 75 attributes are printed. Why is it that some of the attributes are not retrieved? how can i access those attributes?

推荐答案

我认为您需要扩展诸如memberof之类的数组元素.

I think you need to expand array elements like memberof.

尝试一下..可能有帮助.

Try this.. it may help.

Attribute attribute = namingEnumeration.next();
System.out.println(attribute); 
System.out.println(attribute.size()); 

如果尺寸大于1,请再次展开

if size is greater than one.. expand it again

这篇关于使用Spring LdapTemplate从Active Directory获取所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆