You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Kornmod <po...@kornmod.dk> on 2011/04/27 17:36:13 UTC
JNDI, resource and LDAP Trouble
Hi,
I'm trying to create a simple resource for my web-app. My web-app has to
connect to a ldap server to maintain users. I have created a context.xml in
my META-INF folder - with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/LDAP">
<Resource name="blah"
auth="Container"
type="com.sun.jndi.ldap.LdapCtx"
factory="com.sun.jndi.ldap.LdapCtxFactory"
java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
com.sun.jndi.ldap.connect.pool="true"
java.naming.provider.url="ldap://192.168.7.1:389/dc=blah,dc=com"
java.naming.security.authentication="simple"
java.naming.security.principal="cn=blah"
java.naming.security.credentials="blahblah"/>
</Context>
But when I try to access my resource with the following code:
Context newCtx = null;
try {
newCtx = new InitialContext();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
DirContext ctx = (DirContext) newCtx.lookup("blah");
if (ctx != null){
System.out.println ("Success");
} else {
System.out.println ("False");
}
it excepts with the following error:
javax.naming.NameNotFoundException: Name blah is not bound in this Context
I must be missing something essential - or doing something obviously wrong.
But I seem not to be able to crack the nut myself. All help or input is much
appreciated.
NOTE: I have also tried to move the Resource config to
CATALINA/conf/server.xml as a GlobalNamingResource with no luck. The classes
in play obviously is in my CLASSPATH since they are part of my JDK/JRE
(rt.jar). I'm using windows 7 with tomcat 7.0.12 and java 1.6.0.24.
Brgds
/Poul
--
View this message in context: http://old.nabble.com/JNDI%2C-resource-and-LDAP-Trouble-tp31488541p31488541.html
Sent from the Tomcat - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: JNDI, resource and LDAP Trouble
Posted by chris derham <ch...@derham.me.uk>.
>
> I have tried this before - with little success. The code does not through
> any exception now - but the DirContext object (ctx) is null and by that
> unusable:
>
> DirContext ctx = (DirContext)
> newCtx.lookup("java:comp/env/blah");
> if (ctx != null){
> System.out.println ("True");
> } else {
> System.out.println ("False");
> }
>
> The code we use is
// Initialize context.
context = new InitialContext();
Context envCtx = (Context) context.lookup("java:comp/env");
// Initialize object from lookup.
Object object = envCtx.lookup(getJndiLookup());
if (object != null) {
// Initialize session.
return (Session) object;
} else {
// Print debug.
LogLog.debug("Invalid message object returned from JDNI
lookup.");
}
getJndiLokup returns a string, e.g. mail/Session. As long as this matches up
with context.xml resources section, all works. Only problem we've had is
that the classes required for whatever resource need to be in tomcat/lib as
the resource is initialized at tomcat startup.
Are you sure you check all the log files for exceptions?
> I have also tried to list the bindings on comp/env - but there I also get a
> NullPointerException:
>
> NamingEnumeration ne = newCtx.listBindings("java:comp/env");
> while(ne.hasMore()) {
> Binding b = (Binding) ne.next();
> System.out.println(b.getName() + " " + b.getObject());
> }
>
> Any good suggestion to what is going on? I think that I read that you could
> enable debugging on a resource definition - but I seem not to find it any
> again.
>
> NOTE: If I use the more regular approach to connect to the ldap server in
> another servlet - then I have no trouble at all (just to verify the
> credentials):
>
> Hashtable env = new Hashtable();
>
>
> env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
> env.put(Context.PROVIDER_URL, "ldap://
> 192.168.7.1:389/dc=blah,dc=com");
> env.put(Context.SECURITY_AUTHENTICATION, "simple");
> env.put(Context.SECURITY_PRINCIPAL, "cn=blah");
> env.put(Context.SECURITY_CREDENTIALS, "blahblah");
>
> DirContext ctx = null;
> try {
> ctx = new InitialDirContext(env);
> } catch (NamingException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
Our apps use spring security - we have ldap working there and it works a
treat. Don't know if that's an option for you
Chris
Re: JNDI, resource and LDAP Trouble
Posted by Kornmod <po...@kornmod.dk>.
Thanks Chris,
I have tried this before - with little success. The code does not through
any exception now - but the DirContext object (ctx) is null and by that
unusable:
DirContext ctx = (DirContext) newCtx.lookup("java:comp/env/blah");
if (ctx != null){
System.out.println ("True");
} else {
System.out.println ("False");
}
I have also tried to list the bindings on comp/env - but there I also get a
NullPointerException:
NamingEnumeration ne = newCtx.listBindings("java:comp/env");
while(ne.hasMore()) {
Binding b = (Binding) ne.next();
System.out.println(b.getName() + " " + b.getObject());
}
Any good suggestion to what is going on? I think that I read that you could
enable debugging on a resource definition - but I seem not to find it any
again.
NOTE: If I use the more regular approach to connect to the ldap server in
another servlet - then I have no trouble at all (just to verify the
credentials):
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.7.1:389/dc=blah,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=blah");
env.put(Context.SECURITY_CREDENTIALS, "blahblah");
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thanks
/Poul
billybob79 wrote:
>
>
> You should also have a matching entry in web.xml, e.g.
>
> <resource-ref>
> <res-ref-name>blah</res-ref-name>
> <res-type>com.sun.jndi.ldap.LdapCtx</res-type>
> <res-auth>Container</res-auth>
> </resource-ref>
>
> HTH
>
> Chris
>
>
--
View this message in context: http://old.nabble.com/JNDI%2C-resource-and-LDAP-Trouble-tp31488541p31491643.html
Sent from the Tomcat - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: JNDI, resource and LDAP Trouble
Posted by chris derham <ch...@derham.me.uk>.
> But when I try to access my resource with the following code:
>
> Context newCtx = null;
> try {
> newCtx = new InitialContext();
> } catch (NamingException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> try {
> DirContext ctx = (DirContext) newCtx.lookup("blah");
> if (ctx != null){
> System.out.println ("Success");
> } else {
> System.out.println ("False");
> }
>
> Then lookup java:comp/env/blah
You should also have a matching entry in web.xml, e.g.
<resource-ref>
<res-ref-name>blah</res-ref-name>
<res-type>com.sun.jndi.ldap.LdapCtx</res-type>
<res-auth>Container</res-auth>
</resource-ref>
HTH
Chris