Skip to content

Provide ability to set contextSource base through spring.ldap.base property when using embedded LDAP autoconfiguration #10443

@pavelda2

Description

@pavelda2

Spring ldap autoconfiguration provides spring.ldap.base which is used when ContextSource for LdapTemplate is created.

For example:
When I specify spring.ldap.base=dc=company,dc=com I do not need specify whole DN (uid=<username>,dc=company,dc=com) when searching in ldap:

function User findUser(username) {
   return ldapTemplate.lookup("uid=" + username, ldapUserContextMapper);
}

But I get in trouble when testing function above with embedded ldap, because contextSource created with EmbeddedLdapAutoConfiguration does not obtain base property from spring.ldap.base environment property.
Embedded ldap configuration:

spring.ldap.embedded.ldif=classpath:test-ldap.ldif
spring.ldap.embedded.validation.schema=classpath:test-ldap-schema.ldif
spring.ldap.embedded.base-dn=dc=company,dc=com
# spring ldap base is ignored by EmbeddedLdapAutoConfiguration
spring.ldap.base=dc=company,dc=com

Of course, I can provide custom ContextSource bean and setup base programatically for JUnit tests purpose.

@Bean
@ConditionalOnProperty(name = {"spring.ldap.embedded.port", "spring.ldap.base"})
public LdapContextSource contextSource(EmbeddedLdapProperties embeddedProperties, LdapProperties ldapProperties) {
    LdapContextSource ldapContextSource = new LdapContextSource();
    ldapContextSource.setBase(ldapProperties.getBase());
    ldapContextSource.setUrl("ldap://localhost:" + embeddedProperties.getPort());
    return ldapContextSource;
}

But simple one line in application.properties would simplify my JUnit tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions