Java Spring批注@Entry.base中不支持SpEL
我使用SpringDataLDAP和SpringBoot为嵌入式未绑定服务器提供开箱即用的支持。但是,当我使用Spring Data LDAP的Java Spring批注@Entry.base中不支持SpEL,java,spring,spring-boot,spring-el,spring-ldap,Java,Spring,Spring Boot,Spring El,Spring Ldap,我使用SpringDataLDAP和SpringBoot为嵌入式未绑定服务器提供开箱即用的支持。但是,当我使用Spring Data LDAP的@Entry注释时,我需要根据使用的是嵌入式未绑定LDAP服务器还是远程Active Directory服务器,在注释中指定不同的基 我试图通过指定以下内容对基于SpEL和概要文件的属性执行此操作: @Entry(base = "${ldap.person.base}", ...) 然后我有一个application.propreties和ldap.p
@Entry
注释时,我需要根据使用的是嵌入式未绑定LDAP服务器还是远程Active Directory服务器,在注释中指定不同的基
我试图通过指定以下内容对基于SpEL和概要文件的属性执行此操作:
@Entry(base = "${ldap.person.base}", ...)
然后我有一个application.propreties
和ldap.person.base=OU=AD person base
以及一个application embedded.properties
和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
设置为一个或另一个(OU=AD Person base
或OU=Embedded Person base
)还不够吗
EmbeddedLdapAutoConfiguration
和LdapAutoConfiguration
都使用LdapProperties
对象在bean创建期间设置LdapContextSource
上的各种属性,包括其基。据我所知,如果设置了LdapContextSource.base
,则不必为代码库中的每个@条目定义它
如果你没有使用自动配置,如果我的假设是正确的,您应该仍然能够创建自己的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
属性的值对于Active Directory服务器和嵌入的未绑定服务器保持相同,并且可以正常工作。在@PavanKumarJorrigala谢谢-添加了问题链接中有一个未解决的问题。我最近也发现了这一点。当我这样做时,它在嵌入式版本中工作,但我得到javax.naming.PartialResultException:Unprocessed Continuation Reference(s);应用程序对Active Directory执行LDAP搜索时剩余的名称“/”
。如果我在我的Person
条目中指定base=“OU=Domain Users”
,它会工作,但这会破坏嵌入式版本。很好地让它工作!请考虑一下我的回答,因为你显然用了我建议的方法去解决它。
@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;
}
}