Ldap Query - setup using Spring boot

I have a Spring boot application that should execute LDAP requests. I am trying to take the following recommendation from Spring download documentation:

"Many Spring configuration examples are published on the Internet using XML configuration. Always try to use the equivalent Java-base configuration if possible."

In the Spring XML configuration file, I would use:

 <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>

How do I configure this using a Java based configuration? I need to be able to change the attributes of the URL, base, username and password ldap: context-source without restoring the code.

+4
source share
2 answers

<ldap:context-source> XML LdapContextSource bean, <ldap:ldap-template> 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, 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
+18

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>

, !

-2

All Articles