spring - 使用 Spring LdapTemplate 从 Active Directory 获取所有属性

标签 spring spring-boot active-directory ldap spring-ldap

我有一个使用 LDAP 对用户进行身份验证的 Spring Boot 应用程序。对于用户,我正在映射来自 AD 的属性并填充用户的名字、姓氏、部门、电子邮件、电话以及图像等值。但是,我无法从属性中获取员工编号。 当我使用工具 Active Directory explorer 检查属性时,我可以看到每个条目有 88 个属性。但是,当我使用此代码打印上下文中的每个属性时,

@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 个属性。为什么有些属性没有被检索到?我怎样才能访问这些属性?

最佳答案

我认为你需要像 memberof 这样扩展数组元素。

试试这个..它可能会有所帮助。

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

如果 size 大于 1.. 再次展开

关于spring - 使用 Spring LdapTemplate 从 Active Directory 获取所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49400714/

相关文章:

java - 查询结果不存入POJO?

java - 我在 spring/hibernate 中使用什么 mysql 驱动程序?

java - 如何从 Spring Boot 调用用户定义的 sql 函数?

java - 无法使用动态属性键将应用程序属性映射到对象

c# - 我如何在用户的广告中进行递归搜索,无论这是在组还是子组中?

powershell - 如何从 Get-ADComputer 结果中排除特定名称?

javascript - 如何存储点击的超链接的URL

java - Gradle 项目不在我的机器上构建,但在其他人的机器上构建

java - POST 后返回具有 @ManyToOne 关系的已保存实体

Laravel - 如何将 Active Directory 身份验证与用户身份验证结合起来