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

3
我正在尝试对用户进行身份验证,但它抛出了“异常”。可能是配置有问题。
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赋值。

3个回答

2
你正在使用相对专有名称,这将不起作用。
请更改您的代码以使用。
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中始终使用完整的可区分名称。


1
我想告诉你的一件事是,我的 DN: "CN= User Name, CN=Users,dc=organisation,dc=in",而我的sAMAccountNameuserName。我想使用 sAMAccountName 来验证用户。 - rahul
然后,您将需要首先使用代理用户搜索它,以解决FDN。据我所知,在AD中sAMAccountName不是LDAP命名属性,也就是说,您不能使用sAMAccountName=userName,cn=Users,dc=organisation,dc=in - mvreijn

2

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

希望这可以帮到你。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接