我正在尝试使用为 Linux 服务器创建的凭据连接到 Active Directory 服务器。服务器上有一个用于提供凭据的 Web 界面,但我无法使用我提供的凭据进行连接。为了排除故障,我只是尝试使用与服务器上的代码相同的方法在 Java 中从本地计算机进行连接。
public static void main(String[] args) throws NamingException {
final String ldapAdServer = "ldap://server:port";
final String ldapSearchBase = "OU=Users,OU=xxx,DC=yyy,DC=zzz,DC=aaa,DC=com";
final String ldapUsername = "user";
final String ldapPassword = "password";
final String ldapAccountToLookup = "lookup";
final String dc = ",DC=yyy,DC=zzz,DC=aaa,DC=com";
final String ou = ",OU=Uncommon Users,OU=xxx";
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.SECURITY_AUTHENTICATION, "simple");
if(ldapUsername != null) {
env.put(Context.SECURITY_PRINCIPAL, "CN=" + ldapUsername + ou + dc);
}
if(ldapPassword != null) {
env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
}
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapAdServer);
// the following is helpful in debugging errors
//env.put("com.sun.jndi.ldap.trace.ber", System.err);
LdapContext ctx = new InitialLdapContext(env, null);
ConnectLdap ldap = new ConnectLdap();
try {
ldap.findUsers(ctx, ldapSearchBase);
ldap.findAccountByAccountName(ctx, ldapSearchBase, ldapAccountToLookup);
} catch(Exception e) {
e.printStackTrace();
}
}
有很多类似的问题,比如这个 Java ldap authentication issue应使用 cn 而不是 uid。这个Randomly getting LDAP Error 49, data 52e on random users?还有这个LDAP + Java Aplication Authentication error code 49 - 52e AD 不允许匿名绑定(bind),因为我提供了用户名和密码,所以我认为我不会尝试这样做。这个LDAP: error code 49 - Simple Bind Failed: NT_STATUS_LOGON_FAILURE问题是尝试使用相对可分辨名称进行连接,我也不认为我正在这样做。我可以使用此用户名和密码与客户端连接,导航到我连接的用户并获取该用户的完整可分辨名称。这个完整的 dn 是我在 security_principal 中提供的,我缺少什么?
这是尝试构造InitialLdapContext时发生的异常
Exception in thread "main" javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1
最佳答案
这一行
env.put(Context.SECURITY_PRINCIPAL, "CN=" + ldapUsername + ou + dc);
应该是
env.put(Context.SECURITY_PRINCIPAL, ldapUsername@dc1.dc2.dc3.dc4);
如果您使用客户端,则为用户属性中的 userPrincipalName
关于java - 使用 Linux 服务器的凭据连接到 AD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839244/