Spring security 如何使用LdapTemplate获取所有LDAP用户
我正在使用SpringSecurity,希望检索要存储在引用表中的所有用户和组,这样我就可以快速查找用户,而无需查询LDAP目录。我使用以下附加方法创建了一个Spring security 如何使用LdapTemplate获取所有LDAP用户,spring-security,spring-ldap,Spring Security,Spring Ldap,我正在使用SpringSecurity,希望检索要存储在引用表中的所有用户和组,这样我就可以快速查找用户,而无需查询LDAP目录。我使用以下附加方法创建了一个ldapauthoritiespoplator实现镜像defaultldapauthoritiespoplator: public最终集合getAllAuthorities(){ if(groupSearchBase==null){ 返回新的HashSet(); } Set authorities=new HashSet(); 设置角色=l
ldapauthoritiespoplator
实现镜像defaultldapauthoritiespoplator
:
public最终集合getAllAuthorities(){
if(groupSearchBase==null){
返回新的HashSet();
}
Set authorities=new HashSet();
设置角色=ldapTemplate.SearchForSingleAttributeValue(
groupSearchBase,
allAuthorityFilter,
新字符串[0],
组角色属性);
for(字符串角色:角色){
if(convertToUpperCase){
role=role.toUpperCase();
}
添加(新的SimpleGrantedAuthority(rolePrefix+角色));
}
返回当局;
}
这现在允许我检索所有组,allAuthorityFilter
是默认为(&(objectClass=group)(objectCategory=group))的属性
我现在正试图通过以下附加方法,基于ofFilterBasedLdapUserSearch
创建一个自定义的LdapUserSearch
来实现与用户相同的目标:
公共列表findAllUsers(){
SpringSecurityLdapTemplate模板
=新的SpringSecurityLdapTemplate(contextSource);
模板.setSearchControls(searchControls);
列表r=模板.search(searchBase,
allUsersFilter,
新属性映射(){
@凌驾
公共对象mapFromAttributes(属性atrbts)
抛出NamingException{
返回(字符串)atrbts.get(userNameAttribute.get();
}
});
返回r;
}
我在这方面有两个问题:
如果用户列表很大,我会得到javax.naming.sizelimiteExceedeException
,我不知道如何解决这个问题
我希望此方法返回类似于DirContextOperations
的内容,类似于searchForUser(String)
的工作方式,以便可以重用我的LdapUserDetailsMapper
实现以返回所有用户属性
我发现LdapTemplate
的文档有点毛茸茸的,并且在找到我想要的答案时遇到了困难,如果有任何帮助,我将不胜感激
更新:我已通过
public List getAllUserDetails(布尔includeAuthorities){
列表r=新的ArrayList();
for(DirContextOperations ctx:userSearch.findalluserooperations()){
试一试{
Attribute att=ctx.getAttributes().get(usernameAttribute);
字符串用户名=(字符串)att.get();
r、 添加(userMapper.mapUserFromContext)(
ctx,
用户名,
包括授权
?authPop.GetGrantedAuthories(ctx,用户名)
:Collections.emptySet());
}捕获(NamingException-ex){
LOG.warn(“用户名属性”+用户名属性+“未找到!”);
}
}
返回r;
}
在我的UserSearch
实现中,我有:
public List findalluserooperations(){
SpringSecurityLdapTemplate=新的SpringSecurityLdapTemplate(contextSource);
模板.setSearchControls(searchControls);
返回模板。搜索(searchBase,
allUsersFilter,新的ContextMapper(){
@凌驾
公共对象mapFromContext(对象o){
返回(DirContextOperations)o;
}
});
}
然而,我并没有解决第1点。如果我需要以某种方式对其进行批处理,那么只要有一种方法告诉LdapTemplate
在后续调用中在何处继续,就可以了