Spring 注解 @Entry.base 中不支持 SpEL [英] SpEL not supported in Spring annotation @Entry.base

查看:39
本文介绍了Spring 注解 @Entry.base 中不支持 SpEL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Spring Data LDAP and Spring Boot provides out of the box support for an embedded UnboundID server. However, when I use Spring Data LDAP's @Entry annotation, I need to specify a different base in the annotation based on whether I'm using the embedded UnboundID LDAP server, or a remote Active Directory server.

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

I was attempting to do this with SpEL and profile-based properties by specifying:

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

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

Then I have an application.propreties with ldap.person.base=OU=AD Person Base and an application-embedded.properties with ldap.person.base=OU=Embedded Person Base.

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

However, the @Entry annotation does not seem to support SpEL evaluation:

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

javax.naming.InvalidNameException: Invalid name: ${ldap.person.base}

Spring LDAP 中有一个 未解决的问题来添加支持为此,在 Spring LDAP 支持之前,是否有任何解决方法或其他方法可以完成此操作?

There is an open issue in Spring LDAP to add support for this, but is there any workaround or some other way I can accomplish this until it is supported in Spring LDAP?

推荐答案

原来我需要一个不同的 base 的原因首先是因为 Spring 没有设置 baseContextSource 上的 code>.

Turns out the reason I needed a different base in the first place is because Spring was not setting the base on the ContextSource.

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

When you let Spring Boot autoconfigure the embedded LDAP server, it creates a ContextSource as such in 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;
}

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

As you can see, nowhere in there does it call source.setBase(). So to solve this, I added a configuration file with @Profile("embedded") and manually created a ContextSource where I set the base myself (I leave off the credentials part because I don't use credentials for the embedded server):

@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 服务器,并且它可以正常工作.

Now, I can leave the value of the base attribute in my @Entry the same for both the Active Directory server and the embedded UnboundID server and it works properly.

这篇关于Spring 注解 @Entry.base 中不支持 SpEL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆