简体   繁体   English

如何在Spring配置文件中配置ApacheDS工作目录?

[英]How to configure ApacheDS Working Directory in Spring config file?

I have written some unit tests for LDAP authentication using spring security and the embedded ApacheDS server. 我已经编写了一些使用Spring Security和嵌入式ApacheDS服务器进行LDAP身份验证的单元测试。 The server is started up automagically via configuration of the element in the spring application context. 通过在spring应用程序上下文中配置元素,可以自动启动服务器。 I specify an LDIF file for the tests. 我为测试指定了LDIF文件。 All is wonderful. 一切都很棒。 The test ran on our Jenkins instance for a week until one day when the test could not shutdown properly and delete the apacheds-spring-security directory. 该测试在我们的Jenkins实例上运行了一周,直到有一天无法正常关闭并删除apacheds-spring-security目录。 Subsequent test runs will crash due to the presence of this directory. 由于该目录的存在,后续的测试运行将崩溃。

This issue was previously described in 先前在

ApacheDS Embedded with Spring Spring嵌入式ApacheDS

and the answer was to provide a command line argument specifying a working directory location. 答案是提供指定工作目录位置的命令行参数。 If I could do this, I could specify the target directory of my maven build, and maven would take care of the problem for me when it does a clean prior to build and test. 如果可以执行此操作,则可以指定我的Maven构建的目标目录,并且在构建和测试之前进行清理时,maven会为我解决问题。

All good. 都好。 Problem is, how to specify this working directory? 问题是,如何指定此工作目录? -DapacheDSWorkDir="foo" is ignored when I run a maven build, and attempts to set this value programmatically also do not work. 当我运行Maven构建时,-DapacheDSWorkDir =“ foo”被忽略,并且尝试以编程方式设置该值也无效。 Anybody got any ideas? 有人有想法吗? As you can see, the configuration does not include any mention of the underlying LDAP provider. 如您所见,该配置不包含任何底层LDAP提供程序的提及。

    <ldap-server id="ldapServer" 
        url="ldap://localhost:33389/dc=aws,dc=com" 
        manager-dn="uid=admin,ou=system" 
        manager-password="secret"/>

<ldap-server ldif="classpath:/ldap/test-server.ldif"
    root="dc=aws,dc=com"/>

<bean:bean id="federatedLdapUserContextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <bean:constructor-arg value="ldap://localhost:33389/dc=aws,dc=com"/>        
</bean:bean>

Any help would be much appreciated. 任何帮助将非常感激。

Try this (mind the semi-pseudo-code): 试试这个(注意半伪代码):

package spring;

import org.springframework.security.ldap.server.ApacheDSContainer;

public final class EmbeddedApacheDsConfigurer implements BeanPostProcessor {

    private String workingDirectory;

    public void setWorkingDirectory(String workingDirectory) {
        this.workingDirectory = workingDirectory;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApacheDSContainer) {
             ((ApacheDSContainer)bean).setWorkingDirectory(new File(workingDirectory));
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
         return bean;
    }
}

Then in your test bean config: 然后在您的测试bean配置中:

<bean class="spring.EmbeddedApacheDsConfigurer" p:workingDirectory="target/apacheds" />

This assumes Maven's working dir gets set to the root of the project upon running tests. 假设运行测试时,Maven的工作目录已设置为项目的根目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM