java-Spring的SimpleNamingContextBuilder和LDAP
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-Spring的SimpleNamingContextBuilder和LDAP,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4304字,纯文字阅读大概需要7分钟。
内容图文
我目前正在尝试为我们现有的Web应用程序开发一个新模块.我正在尝试添加LDAP功能,并且由于SimpleNamingContextBuilder注册了无法与LdapTemplate一起使用的上下文,因此初始化LDAP上下文时遇到问题.
在我们的spring applicationContext.xml中,我们有几个JNDI查找,因此在运行测试用例之前,我必须在测试用例构造函数中使用SimpleNamingContextBuilder创建模拟JNDI-Resources.
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("someJNDIname",someObject); //e.g. for some datasource
builder.activate();
在我们的Spring 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查找,因此我还会遇到其他问题.
解决方法:
我没有设法让SimpleNamingContextBuilder创建DirContext的实例,但是使用自定义DirContextBuilder是解决此限制的解决方案.
模拟的SimpleNamingContext主要用于通过例如提供绑定对象.
InitialContext.doLookup(String name)
方法-让那些方法起作用并为例如LDAP DirContext实例,只需省略对“已激活”上下文的检查-无论如何您都会引导代码以应用activate(),因此这没有问题-至少对于给定的JNDI LDAP测试用例而言不是.
缺少此检查,代码将寻找“ java.naming.factory.initial”环境键,如果环境为空(InitialContext.doLookup(String name)就是这种情况),您将获得带有绑定对象的模拟SimpleNamingContext.
如果您使用LdapTemplate,则环境不是空的,并且键“ java.naming.factory.initial”设置为“ com.sun.jndi.ldap.LdapCtxFactory”或类似的东西,至少(希望)它是DirContext.
在这种情况下,您可以从createInitialContextFactory调用中获得一个有效的DirContext实例,并且LdapTemplate查找成功.
因此,只需创建一个类DirContextBuilder-从SimpleNamingContextBuilder中获取代码-就像这样:
public class DirContextBuilder implements InitialContextFactoryBuilder {
...
public InitialContextFactory createInitialContextFactory(Hashtable<?,?> environment) {
if (environment != null) {
...
}
省略对激活的== null的检查,就可以测试绑定的JNDI对象并拥有一个可用的LdapTemplate.
内容总结
以上是互联网集市为您收集整理的java-Spring的SimpleNamingContextBuilder和LDAP全部内容,希望文章能够帮你解决java-Spring的SimpleNamingContextBuilder和LDAP所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。