我正在尝试从LDAP服务器检索数据,但它失败了. (连接工程).我很难理解最后一行中search()方法需要哪些参数……
“mail”是我想要获取的信息,userName是经过身份验证的用户.
DirContext authContext = new InitialDirContext(authEnv);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = authContext.search("mail", userName, constraints);
这是我的错误消息(在最后一行出现):
javax.naming.directory.InvalidSearchFilterException: Missing 'equals'; remaining name 'mail'
at com.sun.jndi.ldap.Filter.encodeSimpleFilter(Unknown Source)
at com.sun.jndi.ldap.Filter.encodeFilter(Unknown Source)
at com.sun.jndi.ldap.Filter.encodeFilterString(Unknown Source)
at com.sun.jndi.ldap.LdapClient.search(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.doSearch(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.searchAux(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.c_search(Unknown Source)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(Unknown Source)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(Unknown Source)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(Unknown Source)
at javax.naming.directory.InitialDirContext.search(Unknown Source)
at Client.connect(Client.java:48)
at Client.main(Client.java:23)
感谢所有答案,如果我按要求更改我的代码,我会收到以下错误:
javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0906E9,
comment: In order to perform this operation a successful bind must be completed on the
connection., data 0, v1db1
我的连接代码就是这个:
Properties authEnv = new Properties();
String userName = "XXX";
String passWord = "XXX";
String base = "XXX";
String dn = "uid=" + userName + "," + base;
String ldapURL = "XXX";
authEnv.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
authEnv.put(Context.PROVIDER_URL, ldapURL);
authEnv.put(Context.SECURITY_AUTHENTICATION, "none");
authEnv.put(Context.SECURITY_PRINCIPAL, dn);
authEnv.put(Context.SECURITY_CREDENTIALS, passWord);