2017-05-04 129 views
5

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

我试图通过指定用SpeI和基于配置文件的属性来做到这一点:

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

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

然而,@Entry注释似乎并不支持规划环境地政司评价:

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

有一个open issue在Spring LDAP中添加对此的支持,但是有什么解决方法或其他方式可以完成此操作,直到它在Spring LDAP中受支持为止?

+0

在https://github.com/spring-projects/spring-ldap/issues/444 –

+0

@PavanKumarJorrigala中有一个未解决的问题谢谢 - 添加到问题的链接。我最近也发现了这一点。 –

回答

1

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

当你让春天启动自动配置嵌入式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; 
    } 
} 

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

+0

干得好!请考虑提高我的答案,因为你显然使用了我建议的方法来修复它。 –

2

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

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

如果您没有使用自动配置,并且如果我在我的假设中正确,您仍然应该能够创建您自己的LdapContextSource bean,并将它的base设置为基于Spring属性的期望值。

+0

当我这样做时,它在嵌入式版本中工作,但是我得到了'javax.naming.PartialResultException:未处理的继续引用;当应用程序针对Active Directory执行LDAP搜索时,保留名称'/''。如果我在'Person'条目中指定'base =“OU = Domain Users”',它就可以工作,但是这会打破嵌入式版本。 –