我试图通过LDAP对Active Directory验证用户.以下是我使用的代码片段:
private DirContext bindAsUser(String bindPrincipal, String password) {
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, bindPrincipal);
env.put(Context.PROVIDER_URL, bindUrl);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.REFERRAL, "follow");
try {
return new InitialLdapContext(env, null);
} catch (NamingException e) {
e.printStackTrace()
}
}
Run Code Online (Sandbox Code Playgroud)
如果我提供的话,绑定代码有效:
NetBIOSDomainName\sAMAccountName
(例如域\用户名),或userPrincipalName
(例如username@abc.com),或distinguishedName
(例如CN = username,OU = xxx,DC = abc,DC = com),或objectSid
(例如S-1-5-21-3623811015-3361044348-30300820-1013)因为SECURITY_PRINCIPAL
,如果使用sAMAccountName
(例如用户名)失败(我猜只有森林中唯一的名称是有效的).
那么接受的模式是SECURITY_PRINCIPAL
什么?我搜索了一些类似的问题,但都没有提供官方AD/LDAP文档的参考.或者它是我可以在某处查找的配置?谢谢!