对LDAP和Active Directory不熟悉。我刚刚在Windows Server 2012中安装了与Active Directory相关的功能,并且所有配置都已完成,并且还创建了一个用户。无法通过JAVA中的LDAP连接到Active Directory服务
当我尝试通过LDAP与Active Directory连接时出现以下错误。
Exception in thread "main" javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903C5, comment: AcceptSecurityContext error, data 52e, v2580]
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3067)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3013)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2815)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2729)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:296)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
at com.infomindz.sample.ldap.LDAPTest.main(LDAPTest.java:79)
我已经尝试了一些在网上的文章给出的解决方案,但我仍然得到同样的错误,不知道如何解决这种类型的异常。随着下面的代码只有我在下面的代码行本身很坚持的错误尝试
public static void main(String[] args) throws NamingException
{
final String ldapSearchBase = "dc=sample,dc=com";
final String ldapUsername = "sampleAdminUser";
final String ldapPassword = "password";
final String ldapAccountToLookup = "sampleUser";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://XX.XX.XX.XX:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
if(ldapUsername != null)
{
env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
}
if(ldapPassword != null)
{
env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
}
LdapContext ctx1 = new InitialLdapContext(env, null);
LDAPTest ldap = new LDAPTest();
SearchResult srLdapUser = ldap.findAccountByAccountName(ctx1, ldapSearchBase, ldapAccountToLookup);
System.out.println("srLdapUser :" + srLdapUser);
}
从我的代码:
LdapContext的CTX1 =新InitialLdapContext(ENV,NULL);
如果我在这方面有所不同,请张贴你的上下文。 在此先感谢。
'LDAP:错误代码49'表示无效凭证。你确定他们是正确的吗? – RobAu
@RobAu是的凭据都是正确的。为什么我对此有信心,因为我通过使用相同的用户名和密码登录到系统。 –