You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@directory.apache.org by "Cook, Lee (IS) (Contr)" <R....@ngc.com> on 2010/03/03 21:13:08 UTC

new to ldap for java

Hi there.
Im relatively (its been years) new to LDAP Im sure mine is a basic
newbie problem,
But I need to deliver an LDAP Java coding solution asap.
So...I installed Apache Directory Suite and Studio and loaded the
sevenSeas ldif file,
Brought up Apache Studio, got a connection, scanned the schema entries.
Now, my first LDAP with Java coding try: simply get a context connection
with the
Admin/admin system userid.  After that works I need to be able to
rebind? With other
User/passwords for authentication validation then retrieve a few
attributes.
Alas, my 1st coding attempt fails with :
javax.naming.InvalidNameException: [LDAP: error code 34 - Incorrect DN
given : admin (0x61 0x64 0x6D 0x69 0x6E ) is invalid]
	at
com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2961)
	at
com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2767)
	at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2681)
	at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:310)
	at
com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:190)
	at
com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:208)
	at
com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:
151)
	at
com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:8
1)
	at
javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:679)
	at
javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:259)
	at javax.naming.InitialContext.init(InitialContext.java:235)
	at javax.naming.InitialContext.<init>(InitialContext.java:209)
	at bstt.dlt.ws.impl.LDAPTester.main(LDAPTester.java:35)
Error exiting...2


And here is my code try:
I suspect it may me the provider url?


package bstt.dlt.ws.impl;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class LDAPTester {
	
	
	
	public static void main(String[] args) {
		
		DirContext ctx = null;
		// TODO: init env variable from properties file
		// uid=admin,ou=system  pass=admin
		String sec_authentication = "simple";
		// env.put(Context.SECURITY_PRINCIPAL, "cn=S. User,
ou=NewHires, o=JNDITutorial");
		String sec_principal = "uid=admin, ou=system";
		//String sec_principal = "admin";
		String sec_credentials = "admin";
		// TODO ? provider url needs an extension
ie..."ldap://10.0.1.10:389/dc=ldap,dc=gruchala,dc=eu");
		String provider_url = "ldap://localhost:10389";
		
		// Set up the environment for creating the initial
context
		Hashtable<String, Object> env = new Hashtable<String,
Object>();
		env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, provider_url);
	
env.put(Context.SECURITY_AUTHENTICATION,sec_authentication);
		env.put(Context.SECURITY_PRINCIPAL, sec_credentials);
// specify the username
		env.put(Context.SECURITY_CREDENTIALS, sec_credentials);
// specify the password
		try {
			ctx = new InitialDirContext(env);
			System.out.println(ctx.lookup("cn=admin"));

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//   "LDAP: error code 34 Trying to import users
from the ldap import 
			// gives the error "LDAP: error code 34" Error
code 34 means the dn syntax 
			// in the ldap url is invalid. Consult your ldap
documentation or ldap administrator 
			// for the correct dn syntax." 

			System.err.println("Error exiting...2");
			return;
		}
		

		// ... do something useful with ctx
		System.out.println("... do something useful with ctx");


		
	}
	

}

Lee


Re: new to ldap for java

Posted by Kiran Ayyagari <ay...@gmail.com>.
>  Root DSE (4)
>    Ou=schema
>      Cn= multiple names...are these default schema names for various
>          "sample" or real orgs?
these are the various types of schema available in the server. Note
that some of these
might be disabled by default (check for m-disabled flag on
cn=<schema-name>,ou=schema entry, if that flag is present and has a
value FALSE then
it is diabled if it is TRUE or that attribute is not present then it
is enabled).

> Ou=system...is for the system administrators?

yeah, you can use it for storing any kind of data though :)

Kiran Ayyagari

RE: new to ldap for java

Posted by "Cook, Lee (IS) (Contr)" <R....@ngc.com>.
Thanks Kiran!
I fixed my Cut and paste error setting sec_principal,
Used the install default admin password, fixed ctx.lookup() DN,
As you suggested, and it works now.
Thanks very much. A small but necessary step into LDAP Java for me.
Im ready to tackle the next (small) step.

I would like to also ask you or someone, and use my newbie card once
More, in my LDAP Browser with the default install, I see:
DIT
  Root DSE (4)
    Ou=schema
      Cn= multiple names...are these default schema names for various
          "sample" or real orgs?
Ou=system...is for the system administrators?
Granted I haven't done enough basics reading....

Lee!

-----Original Message-----
From: Kiran Ayyagari [mailto:ayyagarikiran@gmail.com] 
Sent: Wednesday, March 03, 2010 3:25 PM
To: users@directory.apache.org
Subject: Re: new to ldap for java

hi Lee

> env.put(Context.SECURITY_AUTHENTICATION,sec_authentication);
>                env.put(Context.SECURITY_PRINCIPAL, sec_credentials);
set the variable sec_principal in the above line

>                env.put(Context.SECURITY_CREDENTIALS, sec_credentials);
> // specify the password

if you are testing with the default installation then the value of
sec_credentials is "secret" not "admin"
>                try {
>                        ctx = new InitialDirContext(env);
>                        System.out.println(ctx.lookup("cn=admin"));

again if this is tested against default installation change "cn=admin"
to something like "uid=admin,ou=system" or to "ou=system"

HTH

Kiran Ayyagari

Re: new to ldap for java

Posted by Kiran Ayyagari <ay...@gmail.com>.
hi Lee

> env.put(Context.SECURITY_AUTHENTICATION,sec_authentication);
>                env.put(Context.SECURITY_PRINCIPAL, sec_credentials);
set the variable sec_principal in the above line

>                env.put(Context.SECURITY_CREDENTIALS, sec_credentials);
> // specify the password

if you are testing with the default installation then the value of
sec_credentials is "secret" not "admin"
>                try {
>                        ctx = new InitialDirContext(env);
>                        System.out.println(ctx.lookup("cn=admin"));

again if this is tested against default installation change "cn=admin"
to something like "uid=admin,ou=system" or to "ou=system"

HTH

Kiran Ayyagari