【问题标题】:LDAP: error code 49 - Simple Bind Failed: NT_STATUS_LOGON_FAILURELDAP:错误代码 49 - 简单绑定失败:NT_STATUS_LOGON_FAILURE
【发布时间】:2014-08-01 05:07:15
【问题描述】:

我正在尝试对用户进行身份验证,但它抛出了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 一些东西。

【问题讨论】:

    标签: java spring ldap jndi spring-ldap


    【解决方案1】:

    您使用的相对专有名称不起作用。

    更改您的代码以使用

    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 使用完整的专有名称。

    【讨论】:

    • 我想告诉你一件事,我的DN : "CN= User Name, CN=Users,dc=organisation,dc=in" 和我的 'sAMAccountName: userName'。我想使用sAMAccountName 来验证用户身份
    • 然后您必须先使用代理用户搜索它,才能解析 FDN。据我所知,sAMAccountName 不是 AD 中的 LDAP 命名属性,因此您不能使用 sAMAccountName=userName,cn=Users,dc=organisation,dc=in
    【解决方案2】:

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

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      要进行 LDAP 绑定,您将需要为不明确的名称解析条目之一使用唯一返回值之一。通常,人们会使用完全可分辨的名称。

      我们有一个JNDI Example showing how this could be done.

      -吉姆

      【讨论】:

        猜你喜欢
        • 2011-11-29
        • 1970-01-01
        • 2018-02-22
        • 1970-01-01
        • 2018-11-11
        • 2016-11-15
        • 2014-07-04
        • 2019-08-11
        • 2017-06-15
        相关资源
        最近更新 更多