【问题标题】:Integration tests with spring-security and ldap使用 spring-security 和 ldap 进行集成测试
【发布时间】:2012-11-15 11:35:11
【问题描述】:

Spring embedded ldap server in unit tests 类似,但没有给出适合我的答案。

我可以毫无问题地使用 spring 和 spring-security 的嵌入式 ldap 服务器运行我的集成测试。 但是,我还没有找到清除嵌入式 ldap 服务器并重新加载 ldif 以提供通用测试环境的方法。

spring-ldap 的 LdapTestUtils 提供了一个 cleanAndSetup() 方法。但是,这不适用于建议的 apache-ds 版本 (1.5.5),因为 LdifFileLoader 现在需要 CoreSession 而不是 LdapTestUtils 提供的 DirContext。这会导致

java.lang.NoSuchMethodError:
org.apache.directory.server.protocol.shared.store.LdifFileLoader.<init>(Ljavax/naming/directory/DirContext;Ljava/lang/String;)

我只想要一种清除嵌入式 ldap 服务器并再次用 ldif 文件填充它的方法(就像在启动时完成的那样)。 有人对此有任何想法吗?

版本:spring 3.1、spring-ldap 1.3、spring-security 3.1、apache-ds 1.5.5

解决方案(感谢 Luke Taylor):

@Inject
private ApplicationContext applicationContext;

@Before
public void reloadLdapDirectory() throws NamingException, IOException{
    ApacheDSContainer apacheDSContainer = (ApacheDSContainer) applicationContext.getBean(BeanIds.EMBEDDED_APACHE_DS);
    LdapTestUtils.clearSubContexts(contextSource, DistinguishedName.EMPTY_PATH);

    ClassPathResource classPathResource = new ClassPathResource("ldap.ldif");

    File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
    try {
        InputStream inputStream = classPathResource.getInputStream();
        IOUtils.copy(inputStream, new FileOutputStream(tempFile));
        LdifFileLoader fileLoader = new LdifFileLoader(apacheDSContainer.getService().getAdminSession(), tempFile.getAbsolutePath());
        fileLoader.execute();
    }
    finally {
        try {
            tempFile.delete();
        }
        catch (Exception e) {
            // Ignore this
        }
    }
}

【问题讨论】:

标签: spring-security integration-testing spring-ldap


【解决方案1】:

为什么不看看 Spring Security 的 LDAP integration tests 并将其用作指南?

目前,这些只是use an LDAP template 用于清除每个测试在必要时创建的数据(以提高速度),但也有一个注释掉的Junit @After method 会重新加载 LDIF 文件。 CoreSession 是通过在服务器实例上调用getAdminSession() 获得的(DefaultDirectoryService)。

如果您确实必须使用 XML 应用程序上下文运行测试,请使用 &lt;ldap-server /&gt; 元素,您可以使用:

getBeanByName(BeanIds.EMBEDDED_APACHE_DS).getService()

访问DefaultDirectoryService 实例。

【讨论】:

  • 嗨,Luke,首先:与 Spring Security 合作非常棒!我确实看过 LDAP 集成测试,但它们对我没有帮助。我自己没有启动 LDAP 服务器,它是通过 启动的,所以我无法调用 getAdminSession(),因为我没有对 LDAP 服务器的引用。
猜你喜欢
  • 2015-08-12
  • 1970-01-01
  • 2019-08-11
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 2013-03-21
相关资源
最近更新 更多