我試圖讀取Active Directory中的用戶名,但出現錯誤。錯誤是: 異常線程 「main」 javax.naming.AuthenticationException:LDAP:錯誤代碼49 - 80090308:LdapErr:DSID-0C0903AA,註釋:AcceptSecurityContext錯誤,數據525,v1772]閱讀jsp中的Active Directory用戶名稱
然而,我的代碼是:
package client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import javax.naming.*;
import java.util.Hashtable;
import java.util.Enumeration;
public class AD {
public AD() {
super();
}
public static void main(String[] args) throws NamingException {
Hashtable envVars = new Hashtable();
envVars.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
envVars.put(Context.PROVIDER_URL, "ldap://" + "IP:Port");
envVars.put(Context.SECURITY_AUTHENTICATION, "simple");
envVars.put(Context.SECURITY_PRINCIPAL, "username");
envVars.put(Context.SECURITY_CREDENTIALS, "password");
DirContext myContext = new InitialDirContext(envVars);
try{
SearchControls searchCtrls = new SearchControls();
searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attributes = { "cn", "telephoneNumber", "sn", "userPrincipalName","memberOf","name" };
searchCtrls.setReturningAttributes(attributes);
String filter = "(objectClass=organizationalPerson)";
NamingEnumeration values = myContext.search("CN=Users,DC=bma.gov.bh,DC=gov,DC=bh",filter, searchCtrls);
while (values.hasMoreElements())
{
SearchResult result = (SearchResult) values.next();
Attributes attribs = result.getAttributes();
if (null != attribs)
{
for (NamingEnumeration ae = attribs.getAll(); ae.hasMoreElements();)
{
Attribute atr = (Attribute) ae.next();
String attributeID = atr.getID();
for (
Enumeration vals = atr.getAll();
vals.hasMoreElements();
System.out.println(attributeID +": " +vals.nextElement()));
}
}
}
myContext.close();
}
catch(Exception e)
{
//e
}
}
}
問題是從這一行開始:
DirContext myContext = new InitialDirContext(envVars);
能否請你幫
我試圖更換上面的行,它給了我一個錯誤.getBytes – user1080320 2012-02-20 08:48:16
錯誤是什麼? – 2012-02-20 09:20:14
異常無法處理:java.io.UnsupportedEncodingException – user1080320 2012-02-20 09:36:13