Spring annotation @ Entry.base不支持SpEL

And*_*ose 5 java spring spring-ldap spring-el spring-boot

我使用Spring Data LDAP和Spring Boot为嵌入式UnboundID服务器提供开箱即用的支持.但是,当我使用Spring Data LDAP的@Entry注释时,我需要base根据我是使用嵌入式UnboundID LDAP服务器还是远程Active Directory服务器在注释中指定不同的注释.

我试图通过指定以下内容来使用SpEL和基于配置文件的属性执行此操作:

@Entry(base = "${ldap.person.base}", ...)
Run Code Online (Sandbox Code Playgroud)

然后,我有一个application.propretiesldap.person.base=OU=AD Person Baseapplication-embedded.propertiesldap.person.base=OU=Embedded Person Base.

但是,@Entry注释似乎不支持SpEL评估:

javax.naming.InvalidNameException:名称无效:$ {ldap.person.base}

Spring LDAP中有一个未解决的问题,即添加对此的支持,但是在Spring LDAP支持之前,是否有任何解决方法或其他方式可以实现此目的?

And*_*ose 2

事实证明,我base首先需要不同的原因是因为 Spring 没有baseContextSource.

当您让 Spring Boot 自动配置嵌入式 LDAP 服务器时,它会ContextSource在以下位置创建一个EmbeddedLdapAutoConfiguration

@Bean
@DependsOn("directoryServer")
@ConditionalOnMissingBean
public ContextSource ldapContextSource() {
    LdapContextSource source = new LdapContextSource();
    if (hasCredentials(this.embeddedProperties.getCredential())) {
        source.setUserDn(this.embeddedProperties.getCredential().getUsername());
        source.setPassword(this.embeddedProperties.getCredential().getPassword());
    }
    source.setUrls(this.properties.determineUrls(this.environment));
    return source;
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,那里没有任何地方调用source.setBase(). 因此,为了解决这个问题,我添加了一个配置文件,@Profile("embedded")并手动创建了一个ContextSource我自己设置的位置base(我省略了凭据部分,因为我不使用嵌入式服务器的凭据):

@Configuration
@Profile("embedded")
@EnableConfigurationProperties({ LdapProperties.class })
public class EmbeddedLdapConfig {

    private final Environment environment;
    private final LdapProperties properties;

    public EmbeddedLdapConfig(final Environment environment, final LdapProperties properties) {
        this.environment = environment;
        this.properties = properties;
    }

    @Bean
    @DependsOn("directoryServer")
    public ContextSource ldapContextSource() {
        final LdapContextSource source = new LdapContextSource();
        source.setUrls(this.properties.determineUrls(this.environment));
        source.setBase(this.properties.getBase());
        return source;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我可以为 Active Directory 服务器和嵌入式 UnboundID 服务器保留相同的base属性值,并且它可以正常工作。@Entry