我正在嘗試爲我們現有的Web應用程序開發一個新模塊。我試圖添加LDAP功能,並且在初始化LDAP上下文時遇到問題,因爲SimpleNamingContextBuilder註冊了與LdapTemplate不能一起工作的上下文。Spring的SimpleNamingContextBuilder和LDAP
在我們的春天applicationContext.xml
我們有幾個JNDI查找,所以在運行測試案例之前,我必須創建模擬JNDI的資源使用測試用例構造的SimpleNamingContextBuilder。
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("someJNDIname",someObject); //e.g. for some datasource
builder.activate();
在我們的春天application-context-test.xml
我們下面ldapConfiguration:
<bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldap://ourserver:389" />
<property name="base" value="CN=Groups,CN=ourcompany,DC=com" />
<property name="userDn" value="CN=binduser" />
<property name="password" value="password" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="ldapContextSource" />
</bean>
我們經營與測試用例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-context-test.xml"})
public class TestClass {
public TestClass(){
.. //init the SimpleNamingContextBuilder
}
@Autowired
private LdapTemplate template;
@Test
public void someTestcase(){
ldapTemplate.search("", "(objectclass=user)" ,new LdapUserMapper());
}
}
由於SimpleNamingContextBuilder已經註冊一個簡單的InitialContext我得到以下錯誤:
org.springframework.ldap.NotContextException: DirContext object is required.; nested exception is javax.naming.NotContextException: DirContext object is required.
at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:198)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:319)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:259)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:571)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:556)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:411)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:431)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:451)
at com.somecompany.TestClass.someTestcase(TestClass.java:30)
[...]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.naming.NotContextException: DirContext object is required.
at javax.naming.directory.InitialDirContext.castToDirContext(InitialDirContext.java:106)
at javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(InitialDirContext.java:112)
at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:245)
at org.springframework.ldap.core.LdapTemplate$4.executeSearch(LdapTemplate.java:253)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:293)
... 35 more
該錯誤告訴我LDAP需要DirContext
。我怎樣才能讓SimpleNamingContextBuilder創建並使用這樣的DirContext。
如果我沒有註冊SimpleNamingContextBuilder,那麼創建LDAPTemplate
將工作。但是我會遇到其他問題,因爲應用程序的其他部分需要JNDI查找。
這並不能解決混合問題。我想在同一個上下文中初始化數據庫的JNDI數據源。 –
如果你需要在同一個上下文中,這也應該可以使用自定義的DirContextBuilder - 只需創建你自己的DirContext實現,或者使用JBoss one(org.jnp.interfaces.NamingContextFactory)。 –
我需要在相同的背景下,你能告訴我如何完全做到這一點?會真的很感激它!謝謝 –