![](/img/trans.png)
[英]Configure context lookup to find JNDI resource environment on Tomcat 8
[英]Trying to configure LDAP as JNDI Resource in Tomcat
我有一個ldap服務器,我正在使用該服務器對tomcat Web應用程序中的用戶進行身份驗證。 我使用的是JNDIRealm,它在上下文文件中配置,因此效果很好。
我還需要在ldap中搜索用戶信息。 我已經弄清楚了如何使用“ jndi方法”做到這一點,並且通過使用哈希表創建自己的jndi上下文,使其在tomcat之外可以正常工作。 但是,我不想在代碼中配置jndi屬性,而是想在Realm配置旁邊的上下文文件中創建JNDI Rsource。
我想我會做這樣的事情:
<Resource
name="ldap"
auth="Container"
type="com.sun.jndi.ldap.LdapCtxFactory"
java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
java.naming.provider.url="ldap://localhost:389"
java.naming.security.authentication="simple"
java.naming.security.principal="uid=rjcarr,dc=example"
java.naming.security.credentials="abc123"
/>
但是要么tomcat告訴我無法創建資源,要么當我嘗試使用以下方式初始化資源時:
Context initctx = new InitialContext();
DirContext ctx = (DirContext) initctx.lookup("java:comp/env/ldap");
Tomcat告訴我“無法創建資源實例”。 我還在我的web.xml文件中添加了正確的resource-ref,所以我認為這不是問題。
因為LDAP與JNDI方法一起使用,所以我假設它應該能夠配置為Resource,對嗎? 我想念什么?
這個答案有點晚,但是可能對其他用戶有用。 它基於EJP的答案 。
以下解決方案已在Apache Tomcat 7上進行了測試。
如果需要,可以用DirContext
替換LdapContext
。
創建一個實現ObjectFactory
的類以實例化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;
}
}
將以下內容添加到context.xml
,引用工廠並定義值以創建LdapContext
實例:
<?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>
如果需要向資源中添加更多屬性/值,請考慮更新上面創建的ObjectFactory
以讀取這些新屬性/值。
在任何需要的地方注入資源:
@Resource(name = "ldap/LdapResource")
private LdapContext bean;
或查詢一下:
Context initialContext = new InitialContext();
LdapContext ldapContext = (LdapContext)
initialContext.lookup("java:comp/env/ldap/LdapResource");
Apache Tomcat的文檔介紹了如何添加自定義資源工廠 。
你正在編造。 Tomcat資源的類型必須是實現javax.naming.spi.ObjectFactory的類。 有關自定義資源,請參見Tomcat文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.