You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Sebastian Gerdes <se...@informatik.uni-oldenburg.de> on 2007/12/07 02:11:43 UTC

LDAP as resource in context.xml

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

im just working on a web application, where I use a LDAP realm for the
authentification and a postgresql resource in my context.xml. Both are
working fine.

Now I need to access the LDAP in my running web application again. I do
not want to store the user credentials and server information in my
sourcecode. I want to use it as resource like I am used to access my
postgresql database.

In Java I access my database with:

database = (DataSource) ctx.lookup("java:/comp/env/jdbc/pgsql");

It is possible to use the same way to access my LDAP?
If yes, how do I set it up in my context.xml? I searched for example
configurations the whole day and I only found some examples how to
access a database and use LDAP for an authentification.

There must be a way to define it in my context.xml like

<Resource name="ldap/DirContext"
	auth="Container"
	type="com.sun.jndi.ldap.LdapCtxFactory"
	...

How are the further parameters?
Could anybody offer an example.

Many thanks in advance.

Regards,
Sebastian

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHWJ3PDven//UCaP8RApHbAJsExSCKeH3aPGBFtj+9Ibll9IlLLACcDrx/
znlEgMVBDtEGZtuf8J+VMF0=
=XTMG
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: LDAP as resource in context.xml

Posted by Sebastian Gerdes <se...@informatik.uni-oldenburg.de>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

to whom it may concern, I brought my problems to a favorable issue and
it was way easier than I thought.
You just need to write your own resource factory and then you can choose
your parameters as you like. My one looks like this now:

- -----------------------------
<!-- for tomcat 5.5.xx -->

<Resource name="ldap/ox"
auth="Container"
type="com.sun.jndi.ldap.LdapCtx"
factory="drfox.logic.DRFoxContextFactory"
java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
com.sun.jndi.ldap.connect.pool="true"
java.naming.provider.url="ldap://my.server.com:389/dc=my-company,dc=com"
java.naming.security.authentication="simple"
java.naming.security.principal="cn=Manager,dc=my-company,dc=com"
java.naming.security.credentials="password"/>

- ----------------------------

And the java class DRFoxContextFactory to handle it:

- ----------------------------

package drfox.logic;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
import javax.naming.spi.InitialContextFactory;
import javax.naming.directory.InitialDirContext;

public class DRFoxContextFactory implements ObjectFactory,
InitialContextFactory {


    public Object getObjectInstance( Object obj, Name name, Context
nameCtx, Hashtable environment)
    throws NamingException {

        Hashtable env = new Hashtable();
        Reference ref = (Reference) obj;
        Enumeration addrs = ref.getAll();

        while (addrs.hasMoreElements()) {
            RefAddr addr = (RefAddr) addrs.nextElement();
            if (addr.getType().equals("java.naming.factory.initial")) {
                env.put(addr.getType(), addr.getContent().toString());
            } else if (addr.getType().equals("java.naming.provider.url")){
                env.put(addr.getType(), addr.getContent().toString());
            } else if
(addr.getType().equals("java.naming.security.authentication")) {
                env.put(addr.getType(), addr.getContent().toString());
            } else if
(addr.getType().equals("java.naming.security.principal")) {
                env.put(addr.getType(), addr.getContent().toString());
            } else if
(addr.getType().equals("java.naming.security.credentials")) {
                env.put(addr.getType(), addr.getContent().toString());
            } else if
(addr.getType().equals("com.sun.jndi.ldap.connect.pool")) {
                env.put(addr.getType(), addr.getContent().toString());
            }
        }

        return this.getInitialContext(env);
    }

    public Context getInitialContext(Hashtable environment) throws
NamingException {
        return new InitialDirContext(environment);
    }

}

- ---------------------------------


The only thing you need to keep in mind is, that the name of your class
must be the same you defined in your context.xml or server.xml with the
parameter factory="drfox.logic.DRFoxContextFactory"

Now you can access your LDAP via:

Context newCtx = new InitialContext();
Context envCtx = (Context) newCtx.lookup("java:comp/env");
DirContext ctx = (DirContext) envCtx.lookup("ldap/ox");

That's the end of it :)

Kind regards,
Sebastian Gerdes



Sebastian Gerdes schrieb:
> Hello,
> 
> im just working on a web application, where I use a LDAP realm for the
> authentification and a postgresql resource in my context.xml. Both are
> working fine.
> 
> Now I need to access the LDAP in my running web application again. I do
> not want to store the user credentials and server information in my
> sourcecode. I want to use it as resource like I am used to access my
> postgresql database.
> 
> In Java I access my database with:
> 
> database = (DataSource) ctx.lookup("java:/comp/env/jdbc/pgsql");
> 
> It is possible to use the same way to access my LDAP?
> If yes, how do I set it up in my context.xml? I searched for example
> configurations the whole day and I only found some examples how to
> access a database and use LDAP for an authentification.
> 
> There must be a way to define it in my context.xml like
> 
> <Resource name="ldap/DirContext"
> 	auth="Container"
> 	type="com.sun.jndi.ldap.LdapCtxFactory"
> 	...
> 
> How are the further parameters?
> Could anybody offer an example.
> 
> Many thanks in advance.
> 
> Regards,
> Sebastian
> 

- ---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHXYx9Dven//UCaP8RAmgyAJ46neQMdR+Q93G5wZnNz7lu9FFVMgCeOeh2
cd2BaPDGei5aLIavylONFpA=
=6S3Y
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org