Skip to content

Commit 8435aeb

Browse files
nosansnicoll
authored andcommitted
Auto-configure ObjectDirectoryMapper
This commit improves LdapAutoConfiguration to also auto-configure an ObjectDirectoryMapper and configure LdapTemplate to use it. See gh-44290 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent ca1bd3f commit 8435aeb

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfiguration.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,13 +26,18 @@
2626
import org.springframework.boot.autoconfigure.ldap.LdapProperties.Template;
2727
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2828
import org.springframework.boot.context.properties.PropertyMapper;
29+
import org.springframework.boot.convert.ApplicationConversionService;
2930
import org.springframework.context.annotation.Bean;
31+
import org.springframework.core.convert.ConversionService;
3032
import org.springframework.core.env.Environment;
33+
import org.springframework.ldap.convert.ConverterUtils;
3134
import org.springframework.ldap.core.ContextSource;
3235
import org.springframework.ldap.core.LdapOperations;
3336
import org.springframework.ldap.core.LdapTemplate;
3437
import org.springframework.ldap.core.support.DirContextAuthenticationStrategy;
3538
import org.springframework.ldap.core.support.LdapContextSource;
39+
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
40+
import org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper;
3641

3742
/**
3843
* {@link EnableAutoConfiguration Auto-configuration} for LDAP.
@@ -72,10 +77,12 @@ public LdapContextSource ldapContextSource(LdapConnectionDetails connectionDetai
7277

7378
@Bean
7479
@ConditionalOnMissingBean(LdapOperations.class)
75-
public LdapTemplate ldapTemplate(LdapProperties properties, ContextSource contextSource) {
80+
public LdapTemplate ldapTemplate(LdapProperties properties, ContextSource contextSource,
81+
ObjectDirectoryMapper objectDirectoryMapper) {
7682
Template template = properties.getTemplate();
7783
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
7884
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
85+
ldapTemplate.setObjectDirectoryMapper(objectDirectoryMapper);
7986
propertyMapper.from(template.isIgnorePartialResultException())
8087
.to(ldapTemplate::setIgnorePartialResultException);
8188
propertyMapper.from(template.isIgnoreNameNotFoundException()).to(ldapTemplate::setIgnoreNameNotFoundException);
@@ -84,4 +91,18 @@ public LdapTemplate ldapTemplate(LdapProperties properties, ContextSource contex
8491
return ldapTemplate;
8592
}
8693

94+
@Bean
95+
@ConditionalOnMissingBean
96+
public ObjectDirectoryMapper objectDirectoryMapper() {
97+
DefaultObjectDirectoryMapper objectDirectoryMapper = new DefaultObjectDirectoryMapper();
98+
objectDirectoryMapper.setConversionService(createConversionService());
99+
return objectDirectoryMapper;
100+
}
101+
102+
private static ConversionService createConversionService() {
103+
ApplicationConversionService conversionService = new ApplicationConversionService();
104+
ConverterUtils.addDefaultConverters(conversionService);
105+
return conversionService;
106+
}
107+
87108
}

Diff for: spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/LdapAutoConfigurationTests.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,13 @@
1616

1717
package org.springframework.boot.autoconfigure.ldap;
1818

19+
import javax.naming.Name;
20+
21+
import org.assertj.core.api.InstanceOfAssertFactories;
1922
import org.junit.jupiter.api.Test;
2023

2124
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.convert.ApplicationConversionService;
2226
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2327
import org.springframework.context.annotation.Bean;
2428
import org.springframework.context.annotation.Configuration;
@@ -27,6 +31,7 @@
2731
import org.springframework.ldap.core.support.DirContextAuthenticationStrategy;
2832
import org.springframework.ldap.core.support.LdapContextSource;
2933
import org.springframework.ldap.core.support.SimpleDirContextAuthenticationStrategy;
34+
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
3035
import org.springframework.ldap.pool2.factory.PoolConfig;
3136
import org.springframework.ldap.pool2.factory.PooledContextSource;
3237
import org.springframework.ldap.support.LdapUtils;
@@ -194,6 +199,36 @@ void contextSourceWithCustomNonUniqueDirContextAuthenticationStrategy() {
194199
});
195200
}
196201

202+
@Test
203+
void objectDirectoryMapperBeanAutoConfigured() {
204+
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389").run((context) -> {
205+
assertThat(context).hasSingleBean(ObjectDirectoryMapper.class);
206+
assertThat(context).hasSingleBean(LdapTemplate.class);
207+
ObjectDirectoryMapper objectDirectoryMapper = context.getBean(ObjectDirectoryMapper.class);
208+
LdapTemplate ldapTemplate = context.getBean(LdapTemplate.class);
209+
ApplicationConversionService conversionService = assertThat(objectDirectoryMapper)
210+
.extracting("converterManager")
211+
.extracting("conversionService")
212+
.asInstanceOf(InstanceOfAssertFactories.type(ApplicationConversionService.class))
213+
.actual();
214+
assertThat(conversionService.canConvert(String.class, Name.class)).isTrue();
215+
assertThat(conversionService.canConvert(Name.class, String.class)).isTrue();
216+
assertThat(ldapTemplate).extracting("objectDirectoryMapper").isSameAs(objectDirectoryMapper);
217+
});
218+
}
219+
220+
@Test
221+
void customObjectDirectoryMapperBeanCanBeUsed() {
222+
ObjectDirectoryMapper objectDirectoryMapper = mock(ObjectDirectoryMapper.class);
223+
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:389")
224+
.withBean(ObjectDirectoryMapper.class, () -> objectDirectoryMapper)
225+
.run((context) -> {
226+
assertThat(context).hasSingleBean(LdapTemplate.class);
227+
LdapTemplate ldapTemplate = context.getBean(LdapTemplate.class);
228+
assertThat(ldapTemplate).extracting("objectDirectoryMapper").isSameAs(objectDirectoryMapper);
229+
});
230+
}
231+
197232
@Configuration(proxyBeanMethods = false)
198233
static class ConnectionDetailsConfiguration {
199234

0 commit comments

Comments
 (0)