2017-05-04 128 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」',它就可以工作,但是這會打破嵌入式版本。 –