0

为了测试目的,我需要嵌入一个带有 spring 的 LDAP 服务器。以下代码有效:

src/main/webapp/WEB-INF/web.xml

[...]
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        [...]
        com.example.config.servlet.TestLDAPServerConfiguration
    </param-value>
</context-param>
[...]

src/main/java/com/example/config/servlet/TestLDAPServerConfiguration.java

package com.example.config.servlet;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource({"/WEB-INF/test-ldap-server.xml"})
public class TestLDAPServerConfiguration {
}

src/main/webapp/WEB-INF/test-ldap-server.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:s="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

  <s:ldap-server ldif="classpath:users.ldif" root="dc=nestle,dc=com" port="33389"/>
</beans>

我确实需要使用 AnnotationConfigWebApplicationContext 而不是 XmlWebApplicationContext。但是,这里我使用了一个类 TestLDAPServerConfiguration,它只导入了一个 test-ldap-server.xml,而这个 test-ldap-server.xml 只是声明了 ldap-server。

我想删除 test-ldap-server.xml 文件。如何在 TestLDAPServerConfiguration 类中使用 java 代码执行与 s:ldap-server 等效的操作?这在哪里记录?

4

1 回答 1

0

尝试这样的事情;

protected static ApacheDSContainer server;

@BeforeClass
public static void startServer() throws Exception {
    server = new ApacheDSContainer( "dc=yourdomain,dc=com", "classpath:test-server.ldif" );
    server.setPort( 53389 );
    server.afterPropertiesSet();
    server.start();
}

@AfterClass
public static void stopServer() throws Exception {
    if( server != null ) {
        server.stop();
    }
}

您可以从 spring-security-ldap 源代码中获取指南。

于 2014-05-11T09:31:29.310 回答