繁体   English   中英

Spring annotation @ Entry.base不支持SpEL

[英]SpEL not supported in Spring annotation @Entry.base

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

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

@Entry(base = "${ldap.person.base}", ...)

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

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

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

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

我不确定我是否在这里,但假设你在Spring Boot中使用LDAP自动配置,将spring.ldap.base属性spring.ldap.base为一个或另一个( OU=AD Person Base是不够的或OU=Embedded Person Base )根据您使用的个人资料?

EmbeddedLdapAutoConfigurationLdapAutoConfiguration使用LdapProperties对象在bean创建期间设置LdapContextSource各种属性,包括其base 据我所知,如果设置了LdapContextSource.base@Entry为代码库中的每个@Entry定义它。

如果你没有使用自动配置,并且我的假设是正确的,你仍然应该能够创建自己的LdapContextSource bean并根据Spring属性将其base设置为所需的值。

事实证明我首先需要一个不同的base是因为Spring没有在ContextSource上设置base

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

@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;
}

正如你所看到的那样,它无处调用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;
    }
}

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

暂无
暂无

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

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