简体   繁体   中英

Could not connect to Active Directory services through LDAP in JAVA

Am new to LDAP and Active Directory. I have just installed the features related to the Active Directory in Windows Server 2012 and configurations all done and created an User also.

When I try to connect with the Active Directory via LDAP am getting the following error.

Exception in thread "main" javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903C5, comment: AcceptSecurityContext error, data 52e, v2580]
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3067)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3013)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2815)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2729)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:296)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
at com.infomindz.sample.ldap.LDAPTest.main(LDAPTest.java:79)

I have tried some of solutions given in online articles but still am getting the same error, and don't know how to resolve this type of exception. With the below code only I have tried

public static void main(String[] args) throws NamingException
{

    final String ldapSearchBase = "dc=sample,dc=com";

    final String ldapUsername = "sampleAdminUser";
    final String ldapPassword = "password";

    final String ldapAccountToLookup = "sampleUser";


    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://XX.XX.XX.XX:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if(ldapUsername != null)
    {
        env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
    }
    if(ldapPassword != null)
    {
        env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
    }

    LdapContext ctx1 = new InitialLdapContext(env, null);
    LDAPTest ldap = new LDAPTest();

    SearchResult srLdapUser = ldap.findAccountByAccountName(ctx1, ldapSearchBase, ldapAccountToLookup);

    System.out.println("srLdapUser :" + srLdapUser);

}

From my code in the following line of code itself am stuck with the error:

LdapContext ctx1 = new InitialLdapContext(env, null);

Please post your context if I lag in something in this. Thanks in advance.

You need to provide RDN of the user as SECURITY_PRINCIPAL, something like "cn=Administrator" or the complete DN like "cn=Administrator,ou=xyz,dc=infomindz,dc=com".

To cross check you can install an LDAP browser like Apache Directory Studio ( https://directory.apache.org/studio/ ) and try connecting to AD providing RDN or DN of the Administrator user.

By the following updated code I have achieved by fetch the user and the details of the user from Active Directory using LDAP Query

The ldapUsername is the administrator user of the domain controller .

The ldapPassword is the password of the administrator user.

The ldapAccountToLookup is the username to which have to search under the given domain.

public static void main(String[] args) throws NamingException
{

    final String ldapSearchBase = "dc=NewForest,dc=sample,dc=com";

    final String ldapUsername = "sampleAdminUser";
    final String ldapPassword = "Password";

    final String ldapAccountToLookup = "sampleUser";


    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://XX.XX.XX.XX:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
    env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
    DirContext ctx = new InitialDirContext(env);


    LDAPTest ldap = new LDAPTest();

    SearchResult srLdapUser = ldap.findAccountByAccountName(ctx, ldapSearchBase, ldapAccountToLookup);
    System.out.println("srLdapUser :" + srLdapUser);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM