簡體   English   中英

LDAP:錯誤代碼49 - 簡單綁定失敗:NT_STATUS_LOGON_FAILURE

[英]LDAP: error code 49 - Simple Bind Failed: NT_STATUS_LOGON_FAILURE

我試圖驗證用戶,但它拋出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的完整專有名稱。

我們在代碼中遇到了同樣的問題,我們通過在用戶名之前添加域名來修復它。 相反,進入的user:password ,輸入domain\\user:password

希望這可以幫助。

要執行LDAP綁定,您需要使用其中一個“不明確的名稱解析”條目之一返回。 通常,人們會使用完全傑出的名稱。

我們有一個JNDI示例,展示了如何做到這一點。

-Jim

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM