Shi*_*iva 7 jndi spring-mvc active-directory ldap-query spring-ldap
我使用spring-ldap-core-2.3.1.RELEASE.jar了JDK 1.8与Tomcat 8.0通过访问AD信息LdapTemplate.属性如title,department&company不被返回的ldapTemplate.search(..,.,..)方法.
我正在使用以下代码行进行搜索: -
LdapQuery ldapQuery = LdapQueryBuilder.query()
.where("objectclass").is("user")
.and("objectcategory").is("person")
.and("cn").like(strWildcardText+"*");
ldapTemplate.search(ldapQuery, new ADUserAttributesMapper());
Run Code Online (Sandbox Code Playgroud)
以下是ADUserAttributesMapper班级: -
public class ADUserAttributesMapper implements AttributesMapper<ADUserBean> {
@Override
public ADUserBean mapFromAttributes(Attributes attributes) throws NamingException {
if(attributes==null) {
return null;
}
adUserBean.setName((attributes.get("name")!=null) ? attributes.get("name").get().toString() : null);
adUserBean.setCommonName((attributes.get("cn")!=null) ? attributes.get("cn").get().toString() : null);
adUserBean.setDisplayName((attributes.get("displayname")!=null) ? attributes.get("displayname").get().toString() : null);
adUserBean.setGivenName((attributes.get("givenname")!=null) ? attributes.get("givenname").get().toString() : null); // for FIRST NAME
adUserBean.setMiddleName((attributes.get("initials")!=null) ? attributes.get("initials").get().toString() : null); // for MIDDLE NAME / INITIALS
adUserBean.setLastName((attributes.get("sn")!=null) ? attributes.get("sn").get().toString() : null); // for LAST NAME
adUserBean.setDepartment((attributes.get("department")!=null) ? attributes.get("department").get().toString() : null);
adUserBean.setUserPrincipalName((attributes.get("userprincipalname")!=null) ? attributes.get("userprincipalname").get().toString() : null); // Logon Name
adUserBean.setsAMAccountName((attributes.get("samaccountname")!=null) ? attributes.get("samaccountname").get().toString() : null); // Logon Name (pre-Windows 2000)
adUserBean.setDistinguishedName((attributes.get("distinguishedname")!=null) ? attributes.get("distinguishedname").get().toString() : null);
adUserBean.setMailID((attributes.get("mail")!=null) ? attributes.get("mail").get().toString() : null);
adUserBean.setTitle((attributes.get("title")!=null) ? attributes.get("title").get().toString() : null); // Job Title
adUserBean.setTelephoneNumber((attributes.get("telephonenumber")!=null) ? attributes.get("telephonenumber").get().toString() : null);
adUserBean.setObjectCategory((attributes.get("objectcategory")!=null) ? attributes.get("objectcategory").get().toString() : null);
return adUserBean;
}
}
Run Code Online (Sandbox Code Playgroud)
的title,department与company属性属于组织的AD用户属性的选项卡,如图以下图像中: -

此外,在General选项卡中initials,Spring-LDAP没有提取/列出initials()属性ldapTemplate.该LdapQueryBuilder.query()对象可以访问attributes(...)方法,该方法接受要获取的属性名称的字符串数组.但是,即使不谈有明确后,值的属性,如initials,title,department和company不返回.
在LDAP浏览器在Eclipse IDE中的插件列出title,department与company下属性组织选项卡没有问题.
即使com4jAPI返回title,department和company属性.
是否存在限制属性列表的任何配置,或者它是Spring-LDAP API本身的限制?这些属性不属于BasicAttributes?如何通过Spring-LDAP获取这些属性?
UPDATE(01 - 8月- 2017年):
在普通的Java JNDI方法/代码不会返回department,company,title属性(即使这些属性正在属性字符串数组中明确提到),但意外的是不返回的initials属性值.
更新(2017年8月2日):
类似于@Pierre的建议(下面)尝试使用SearchControls对象的以下代码: -
String strFilter= "(&(objectclass=top)(cn=cgma*))";
String[] attrs = new String[] {"cn","givenName","sn","initials","title","department","company"};
long maxResults = 10; // for example
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setReturningAttributes(attrs);
searchControls.setCountLimit(maxResults);
List<String> aLstOfADUsers = ldapTemplate.search("",strFilter,searchControls,new AttributesMapper<String>()
{
public String mapFromAttributes(Attributes attrs) throws NamingException {
try
{
System.out.println(attrs.toString());
return attrs.get("cn").get().toString();
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
});
return aLstOfADUsers;
Run Code Online (Sandbox Code Playgroud)
即使这不退还initials,title,company和department属性值.
人物属性可能是内部属性,默认情况下您不会收回。您可以在所使用的搜索方法(传递LdapQuery对象的方法)中明确指定要返回的属性,但不返回。如果您看一下org.springframework.ldap.core.LdapTemplate类,似乎您无法将SearchControls对象传递给所使用的方法签名。因此,为了能够指定要获取的属性,请替换为:
LdapQuery ldapQuery = LdapQueryBuilder.query()
.where("objectclass").is("user")
.and("objectcategory").is("person")
.and("cn").like(strWildcardText+"*");
ldapTemplate.search(ldapQuery, new ADUserAttributesMapper());
Run Code Online (Sandbox Code Playgroud)
有了这个:
LikeFilter filter = new LikeFilter("cn", strWildcardText+"*");
// list of attributes to retrieve
String[] attrs = new String[] {"title","department","company"};
long maxResults = 10; // for example
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setReturningAttributes(attrs);
searchControls.setCountLimit(numResults);
ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), searchControls, new ADUserAttributesMapper());
Run Code Online (Sandbox Code Playgroud)
以上应该可以。您也可以尝试这样的操作(我还没有尝试过):
ldapTemplate.search( "dc=yourorg,dc=com",
"(&(cn=" +strWildcardText + "*)(&(objectClass=person)(objectcategory=person)))",
SearchControls.SUBTREE_SCOPE,
new String[]{ "title","department","company" },
new ADUserAttributesMapper() );
Run Code Online (Sandbox Code Playgroud)
最后,要取回所有属性,请要求在上面的代码中检索所有属性(我的上述示例仅询问了3个属性,这将返回所有属性):
String[] attrs = new String[]{"*","+"};
Run Code Online (Sandbox Code Playgroud)
这是基于你的AttributesMapper. 我不知道ADUserAttributesMapper是什么,所以你必须提供该实现。
这是该接口的 javadoc。 http://docs.spring.io/spring-ldap/docs/current/apidocs/org/springframework/ldap/core/AttributesMapper.html
| 归档时间: |
|
| 查看次数: |
2959 次 |
| 最近记录: |