【发布时间】:2015-10-06 11:41:30
【问题描述】:
我编写了一个测试代码,用于通过 Active Directory 服务器对用户进行身份验证。我可以使用下面的代码使用绑定 dn 进行身份验证。
public static void main(String[] args) {
LdapContext ldapContext = null;
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://10.121.85.24:636");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_PRINCIPAL, "EXTLDAPTEST\batty"); // line 1
env.put(Context.SECURITY_CREDENTIALS, "mypassword");
env.put("com.sun.jndi.ldap.read.timeout", Integer.toString(8000));
env.put("java.naming.ldap.factory.socket", "com.auth.server.TrustAllSSLSocketFactory" );
try {
ldapContext = new InitialLdapContext(env, null);
} catch (Exception e) {
e.printStackTrace();
}
if (ldapContext != null)
{
System.out.println("Authenticatied");
}
}
但是当我用
替换第 1 行时env.put(Context.SECURITY_PRINCIPAL, "CN=batty,OU=Unsorted,OU=EDN Users,OU=User accounts,DC=extLDAPTest,DC=local"); // line 1
抛出异常
javax.naming.AuthenticationException:[LDAP:错误代码 49 - 80090308:LdapErr:DSID-0C0903A9,注释:AcceptSecurityContext 错误,数据 52e,v1db1
AD的树结构是:
我在尝试使用完整 dn 进行身份验证时做错了吗?
编辑 1: 当我使用服务帐户获取完整的 dn 使用
NamingEnumeration<?> aa = context.list("OU=Unsorted,OU=EDN Users,OU=User accounts,DC=extLDAPTest,DC=local");
我得到以下结果:
CN=batty,OU=Unsorted,OU=EDN 用户,OU=用户帐户,DC=extLDAPTest,DC=local
这与我通过身份验证相同。
编辑 2 :我使用完整 dn 的原因是,我将获得服务帐户和子树的 dn。现在,同一个用户可以存在于不同的子树中。所以我想从特定的子树中对其进行身份验证。
【问题讨论】:
标签: java active-directory ldap