gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:35:39 37 4
gpt4 key购买 nike

我有一个使用 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/

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