我目前正在尝试使用嵌入式ldap服务器进行单元测试.
在Spring Security中,您可以快速定义嵌入式ldap服务器,以便使用从指定的ldif加载的一些示例数据对标记进行测试.
我将使用Spring Ldap执行ldap操作,并考虑测试用户服务对象的常用CRUD功能.
但是,有没有办法确保嵌入式服务器中的条目处于相同的一致状态(有点像删除所有并重新加载ldif条目)我正在运行的每个测试?
我想到了以下内容:1)表明该方法污染了上下文,并强制重新创建嵌入式ldap服务器,这听起来很痛苦,因为它必须为每个方法重新启动服务器2)在测试组织中创建测试条目,这样我就可以取消绑定它们,然后再在那里再次加载ldif文件.
我更喜欢2,但似乎Spring LDAP没有好的帮助器来加载和发送ldif文件的内容.
有关如何使用spring的嵌入式ldap服务器或我提到的两种可能解决方案执行ldap测试的任何建议?
谢谢
上一次我向我们的项目中添加了另一个身份验证提供程序,以便通过Windows Active Directory服务器对用户进行身份验证:
<security:authentication-manager id="authenticationManager" erase-credentials="true">
<security:authentication-provider ref="ldapActiveDirectoryAuthProvider" />
<security:authentication-provider ref="authenticationProvider1"/>
<security:authentication-provider ref="authenticationProvider2"/>
</security:authentication-manager>
<bean id="customLdapUserDetailsMapper" class="security.authentication.customLdapUserDetailsMapper">
</bean>
<bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="my.domain"/>
<constructor-arg value="ldap://my.custom.host:389" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
<property name="userDetailsContextMapper" ref="customLdapUserDetailsMapper" />
</bean>
Run Code Online (Sandbox Code Playgroud)
除了与身份验证流程一起使用的现有集成测试以外,几乎一切正常。即,每个测试都尝试在ActiveDirectoryLdapAuthenticationProvider.bindAsUser然后失败时尝试连接到服务器,因为my.custom.host无法用于此类型的测试。
我已经开始谷歌搜索,以便为这种类型的测试找到一些模拟,但是不幸的是,我只找到了这篇有关spring-security和ldap的集成测试,其中Luke Taylor建议使用现有的集成测试作为指南。我已经研究了它,但其中不包含针对此类提供程序的任何测试。
我是新手,很高兴知道以下几点:
new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");以LDAP集成测试中提到的方法以任何方式重用是否正确(我不确定它是否适合我,因为我没有在应用程序上下文中创建ldap ebbedded ldap服务器,也未指定任何方法。提及配置中的ldif文件)。