javaweb应用中的LDAP认证

javaweb应用中的LDAP认证,java,active-directory,ldap,Java,Active Directory,Ldap,我正在尝试验证LDAP中用户的用户ID和密码这是我的java代码 Hashtable<Object, String> props = new Hashtable<Object, String>(); //props.put(Context.SECURITY_AUTHENTICATION, "simple"); props.put(Context.SECURITY_PRINCIPAL, "uid=muhammad.zafar,ou=IT Operations

我正在尝试验证LDAP中用户的用户ID和密码这是我的java代码

Hashtable<Object, String> props = new Hashtable<Object, String>();
//props.put(Context.SECURITY_AUTHENTICATION, "simple");
props.put(Context.SECURITY_PRINCIPAL,
        "uid=muhammad.zafar,ou=IT Operations,ou=Information Systems,dc=bi,dc=com,dc=pk");  
props.put(Context.SECURITY_CREDENTIALS, "Passw0rd");
DirContext context;

try {

    context = com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance("ldap://ldap.bi.com.pk:389/dc=bi,dc=com,dc=pk" + '/', props);
    context.close();
} catch (Exception e) {
    throw new BadCredentialsException("Invalid Username or Password");
}
它抛出异常“javax.naming.AuthenticationException:[LDAP:错误代码49-无效凭据]” 甚至我也试着把它放进去

props.put(Context.SECURITY_AUTHENTICATION, "simple");

他们都不为我工作。我希望我的问题很清楚,因为我的应用程序中没有每个用户都是“ou”。

试试这个解决方案,希望它对您有用

这不是您想使用
ou
进行身份验证还是不使用它的问题

事实上,
simple
authentication模式(请参阅)中的LDAP协议和JNDI要求您将用户的dn设置为
SECURITY\u PRINCIPAL

所以你有两种方法来解决这个问题:

  • 使用技术帐户搜索尝试进行身份验证的用户的DN,然后使用以前找到的DN进行身份验证

  • 不要使用
    simple
    身份验证机制,而是使用目录支持的SASL机制,该机制允许您使用dn以外的值

    有关更多信息,请参见此:


请提供以下示例:“不要使用简单的身份验证机制,而是目录支持的SASL机制,允许您使用dn以外的值。”@Jahanger我发布的文档中有基于摘要、Kerberos、CRAM MD5等身份验证示例的链接。
props.put(Context.SECURITY_AUTHENTICATION, "simple");