背景
项目中需要根据配置自定义ldap连接池,不能直接采用spring-boot-starter-data-ldap的自动注入。
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
启动类排除自动注入
@SpringBootApplication(exclude = LdapAutoConfiguration.class)
@Bean
public ContextSource ldapContextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://ip:1666");
contextSource.setBase("c=cn");
contextSource.setUserDn("cn=admin,c=cn");
contextSource.setPassword("111111");
contextSource.setPooled(true);
contextSource.afterPropertiesSet();
PoolingContextSource poolingContextSource = new PoolingContextSource();
poolingContextSource.setDirContextValidator(new DefaultDirContextValidator());
poolingContextSource.setContextSource(contextSource);
poolingContextSource.setTestOnBorrow(true);
poolingContextSource.setTestWhileIdle(true);
TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(poolingContextSource);
return proxy;
}