【问题标题】:Ldap Query - Configuration using Spring BootLdap 查询 - 使用 Spring Boot 进行配置
【发布时间】:2014-11-18 04:32:04
【问题描述】:

我有一个需要执行 LDAP 查询的 Spring Boot 应用程序。我正在尝试从 Spring Boot 文档中获取以下建议:

“很多Spring配置示例已经发布在 使用 XML 配置的 Internet。始终尝试使用等效的 如果可能,基于 Java 的配置。”

在 Spring XML 配置文件中,我会使用:

 <ldap:context-source
          url="ldap://localhost:389"
          base="cn=Users,dc=test,dc=local"
          username="cn=testUser"
          password="testPass" />

   <ldap:ldap-template id="ldapTemplate" />

   <bean id="personRepo" class="com.llpf.ldap.PersonRepoImpl">
      <property name="ldapTemplate" ref="ldapTemplate" />
   </bean>

如何使用基于 Java 的配置进行配置?我需要能够更改 ldap:context-source 的 URL、base、用户名和密码属性,而无需重新构建代码。

【问题讨论】:

    标签: spring-boot spring-ldap


    【解决方案1】:

    &lt;ldap:context-source&gt; XML 标记产生一个LdapContextSource bean,&lt;ldap:ldap-template&gt; XML 标记产生一个LdapTemplate bean,所以这就是您需要在 Java 配置中执行的操作:

    @Configuration
    @EnableAutoConfiguration
    @EnableConfigurationProperties
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Bean
        @ConfigurationProperties(prefix="ldap.contextSource")
        public LdapContextSource contextSource() {
            LdapContextSource contextSource = new LdapContextSource();
            return contextSource;
        }
    
        @Bean
        public LdapTemplate ldapTemplate(ContextSource contextSource) {
            return new LdapTemplate(contextSource);
        }
    
        @Bean 
        public PersonRepoImpl personRepo(LdapTemplate ldapTemplate) {
            PersonRepoImpl personRepo = new PersonRepoImpl();
            personRepo.setLdapTemplate(ldapTemplate);
            return personRepo;
        }
    }
    

    为了让您在不重新构建代码的情况下更改配置,我使用了 Spring Boot 的@ConfigurationProperties。这将在您的应用程序环境中查找以ldap.contextSource 为前缀的属性,然后通过调用匹配的setter 方法将它们应用到LdapContextSource bean。要在问题中应用配置,您可以使用具有四个属性的application.properties 文件:

    ldap.contextSource.url=ldap://localhost:389
    ldap.contextSource.base=cn=Users,dc=test,dc=local
    ldap.contextSource.userDn=cn=testUser
    ldap.contextSource.password=testPass
    

    【讨论】:

    • 奇怪 - 如果我创建一个 yaml 配置文件,我无法让它工作 - 当我创建一个属性配置时它工作正常。
    • 它也适用于 yaml 配置文件。 ldap.contextSource: url: ldap://localhost:389 base: cn=Users,dc=test,dc=local userDn: cn=testUser password: testPass
    • 不需要第 3 个 bean,如果你离开它,repo 不需要 public 修饰符,但可以留在包范围内(更适合域驱动设计)
    • 如果没有上述第三种@Bean 方法,如何在问题的XML 配置中创建personRepo bean?
    【解决方案2】:

    在 XML 中,您可以使用:

    <bean  id="ldapContextSource" 
            class="org.springframework.ldap.core.support.LdapContextSource">
        <property name="url" value="<URL>" />
        <property name="base" value="<BASE>" />
        <property name="userDn" value="CN=Bla,OU=com" />
        <property name="password" value="<PASS>" />
        <property name="referral" value="follow" />
    </bean>
    
    <bean   id="ldapTemplate" 
            class="org.springframework.ldap.core.LdapTemplate" 
            depends-on="ldapContextSource">
            <constructor-arg index="0" ref="ldapContextSource" />
            <property name="ignorePartialResultException" value="true"/>
    </bean>
    
    <bean id="personRepo" class="com.llpf.ldap.PersonRepoImpl">
            <property name="ldapTemplate" ref="ldapTemplate" />
    </bean>
    

    希望对你有帮助!

    【讨论】:

    • 没用。这完全不是他想要的。
    猜你喜欢
    • 2017-10-20
    • 2019-03-09
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    相关资源
    最近更新 更多