【问题标题】:LDAP authentication in JAVA web applicationJAVA Web 应用程序中的 LDAP 身份验证
【发布时间】:2017-05-31 08:54:45
【问题描述】:

我正在尝试从 LDAP 验证用户的用户名和密码,这是我的 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");
}

由于用户 muhammad.zafar 存在于 IT 操作“ou”中,因此效果很好。但我不想用它的 ou 验证用户,所以我尝试了 SECURITY_PRINCIPAL 的许多设置,但没有一个对我有用

props.put(Context.SECURITY_PRINCIPAL,
                "uid=muhammad.zafar");

它抛出异常“javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]” 甚至我试图放入

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

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

【问题讨论】:

    标签: java active-directory ldap


    【解决方案1】:

    试试这个解决方案,希望它对你有用 possible solution

    【讨论】:

      【解决方案2】:

      这不是您是否要使用ou 进行身份验证的问题。

      事实上,simple 身份验证模式下的 LDAP 协议和 JNDI(参见 doc)要求您将用户的 dn 设置为 SECURITY_PRINCIPAL

      所以你有两种方法来处理它:

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

      • 不要使用simple 身份验证机制,而是您的目录支持的一种 SASL,它允许您使用除 dn 之外的其他值。

        查看更多信息:https://docs.oracle.com/javase/tutorial/jndi/ldap/sasl.html

      【讨论】:

      • 能否请为此“不要使用简单的身份验证机制,而是您的目录支持的 SASL 机制,它允许您使用除 dn 之外的其他值。”提供示例。
      • @Jahangeer 我发布的文档中包含基于 Digest、Kerberos、CRAM MD5 等的身份验证示例的链接。
      猜你喜欢
      • 1970-01-01
      • 2012-08-03
      • 2016-04-30
      • 2018-04-15
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      相关资源
      最近更新 更多