This answer is a bit late, but probably it'll be useful for other users. It's based on EJP's answer.
The following solution was tested on Apache Tomcat 7.
If you need, you can replace LdapContext
with DirContext
.
Create an ObjectFactory
Create a class which implements ObjectFactory
to instantiate a LdapContext
:
public class LdapContextFactory implements ObjectFactory {
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable<?, ?> environment) throws Exception {
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
Reference reference = (Reference) obj;
Enumeration<RefAddr> references = reference.getAll();
while (references.hasMoreElements()) {
RefAddr address = references.nextElement();
String type = address.getType();
String content = (String) address.getContent();
switch (type) {
case Context.INITIAL_CONTEXT_FACTORY:
env.put(Context.INITIAL_CONTEXT_FACTORY, content);
break;
case Context.PROVIDER_URL:
env.put(Context.PROVIDER_URL, content);
break;
case Context.SECURITY_AUTHENTICATION:
env.put(Context.SECURITY_AUTHENTICATION, content);
break;
case Context.SECURITY_PRINCIPAL:
env.put(Context.SECURITY_PRINCIPAL, content);
break;
case Context.SECURITY_CREDENTIALS:
env.put(Context.SECURITY_CREDENTIALS, content);
break;
default:
break;
}
}
LdapContext context = new InitialLdapContext(env, null);
return context;
}
}
Define your resource
Add the following to your context.xml
, referencing the factory and defining the values to create a LdapContext
instance:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
...
<Resource name="ldap/LdapResource" auth="Container"
type="javax.naming.ldap.LdapContext"
factory="com.company.LdapContextFactory"
singleton="false"
java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
java.naming.provider.url="ldap://127.0.0.1:389"
java.naming.security.authentication="simple"
java.naming.security.principal="username"
java.naming.security.credentials="password" />
</Context>
If you need to add more attributes/values to your resource, consider updating your ObjectFactory
created above to read these new attributes/values.
Use your resource
Inject your resource wherever you need:
@Resource(name = "ldap/LdapResource")
private LdapContext bean;
Or look it up:
Context initialContext = new InitialContext();
LdapContext ldapContext = (LdapContext)
initialContext.lookup("java:comp/env/ldap/LdapResource");
See more
Apache Tomcat's documentation explains how to add custom resource factories.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…