【问题标题】:Get all attributes from Active Directory using Spring LdapTemplate使用 Spring LdapTemplate 从 Active Directory 获取所有属性
【发布时间】:2018-08-30 05:47:29
【问题描述】:

我有一个使用 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 个属性。为什么有些属性没有检索到?我如何访问这些属性?

【问题讨论】:

  • employeeNumber 属性是否有值?它通常只获取有值的属性。
  • 是的,它确实有价值。
  • 如果它可以帮助某人,这就是我为解决这个问题所做的。我是连接到 3268 端口,现在我已将其更改为 389 端口。阅读此link 以了解两个端口之间的差异。请注意,这要慢得多。然后,我将 groupSearchBase 更新为更具体。 (之前只有 DC=mydomain,DC=com,现在是 OU=mygroup,DC=mydomain,DC=com)
  • 啊,是的。这就说得通了。默认情况下,该属性不会复制到全局编录。如果需要,您可以将其复制到 GC。这涉及更新 Schema 中的属性。
  • @K.G 我渴望做一些非常相似的事情。你有完整课程的链接吗?这是上什么课的?

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


【解决方案1】:

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

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

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

如果大小大于一.. 再次展开

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多