LDAP:错误代码49 – 简单绑定失败:NT_STATUS_LOGON_FAILURE

我试图validation用户,但它抛出Exception可能是配置有问题。

 public class LdapApplication { private static final String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; private static final String SECURITY_AUTHENTICATION ="simple"; private static final String NAMED_CONTEXT = "CN=Users"; private static final String SAM_ACCOUNT_NAME = "sAMAccountName="; public static void main(String[] args) { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY); env.put(Context.PROVIDER_URL, "ldap://ip:portNo/dc=organisation,dc=in"); env.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION); env.put(Context.SECURITY_PRINCIPAL, "cn=userName,cn=Users"); env.put(Context.SECURITY_CREDENTIALS, "password" ); DirContext context = null; NamingEnumeration namingEnumeration = null; try { context = new InitialDirContext(env); namingEnumeration = context.search(NAMED_CONTEXT, SAM_ACCOUNT_NAME+ userName, null); while (namingEnumeration.hasMore()) { SearchResult searchResult = (SearchResult) namingEnumeration.next(); Attributes attributes = searchResult.getAttributes(); System.out.println(" Person Common Name = " + attributes.get("cn")); System.out.println(" Person Display Name = " + attributes.get("displayName")); }catch(Exception e){ System.out.println(e.getMessage()); e.printStackTrace(); } } } catch (Throwable e) { e.printStackTrace(); } finally { if (namingEnumeration != null) { try { namingEnumeration.close(); } catch (Exception e) { } } if (context != null) { try { context.close(); } catch (Exception e) { } } } } 

}

但如果我提到Context.SECURITY_PRINCIPAL"organisation\\userName"而不是"cn=userName,cn=Users"它的工作原理非常好。 请建议一个可能的解决方案,因为我的要求是使用cn或dc给SECURITY_PRINCIPAL一些东西。

您使用的是相对可分辨名称,但不起作用。

更改要使用的代码

 env.put(Context.SECURITY_PRINCIPAL, "cn=userName,cn=Users,dc=organisation,dc=in"); 

并将您的搜索上下文更改为:

 private static final String NAMED_CONTEXT = "CN=Users,dc=organisation,dc=in"; 

始终使用LDAP的完整专有名称。

我们在代码中遇到了同样的问题,我们通过在用户名之前添加域名来修复它。 输入domain\user:password ,而不是输入domain\user:password

希望这可以帮助。

要执行LDAP绑定,您需要使用其中一个“不明确的名称解析”条目之一返回。 通常,人们会使用完全杰出的名称。

我们有一个JNDI示例,展示了如何做到这一点。

-Jim