- 配置LdapTemplate
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class LdapBeansConfig {
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
final LdapProperties ldapProperties = securityProperties.getServer().getLdap();
contextSource.setUrl("ldap://host:389");
contextSource.setBase("dc=dc,dc=com");
contextSource.setUserDn("userdn");
contextSource.setPassword("password");
contextSource.setPooled(true);
// 乱码问题
Map<String, Object> config = new HashMap<>();
config.put("java.naming.ldap.attributes.binary", "objectGUID");
contextSource.setBaseEnvironmentProperties(config);
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate(LdapContextSource contextSource) {
return new LdapTemplate(contextSource);
}
}
使用:参考 Spring LDAP Reference