一、连接池是什么?
连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。
都应该使用过数据连接池,例如druid,c3p0 大致原理差不多,刚开始使用还是麻烦的,网上的文档什么信息都不全,找了好久自己研究才搞明白
二、使用步骤
1.引入库
版本就是随着springBoot来吧
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
2.配置连接池数据源
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ldap.LdapProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import java.util.Collections;
/**
* 配置AD数据源
*/
@Configuration
@EnableConfigurationProperties(LdapProperties.class)
public class LdapConfig {
@Autowired
private LdapProperties properties;
@Autowired
private Environment environment;
@Bean
public LdapContextSource ldapContextSource() {
LdapContextSource source = new LdapContextSource();
source.setUserDn(properties.getUsername());
source.setPassword(properties.getPassword());
source.setBase(properties.getBase());
source.setUrls(properties.determineUrls(environment));
source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment()));
return source;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(ldapContextSource());
}
}
很多配置官网都有,我就是使用了默认的配置
官网地址:https://docs.spring.io/spring-ldap/docs/2.3.2.RELEASE/reference/#basic-authentication
三、实际使用
使用起来也挺简单的,就当作一个service来用,把ldapTemplate注入进来
public class LdapServiceImpl implements LdapService {
private static final String TAG = "----- LdapServiceImpl -----";
@Autowired
private LdapTemplate ldapTemplate;
/**
* @param password 账号
* @param password 密码
*/
public UserResponse ldapAuth(String username, String password) {
//查询账号
Person person = ldapTemplate.findOne(query().base("OU=XXX").where("XXXX").is(CommonUtil.escapeQueryChars(username)), Person.class);
//加入过滤条件
EqualsFilter filter = new EqualsFilter("XXXX", XXXX);
//验证密码
Boolean result = ldapTemplate.authenticate("OU=XXXX", filter.toString(), password);
return new UserResponse(person.getCn(), person.getMail(), username);
}
Ps: CommonUtil 这个有ldap注入的问题,我也是安全组给提了bug才了解的,希望不知道的了解下,我解决的方式不是最好的,但是也是可以用的
public static String escapeQueryChars(String s) {
if (StringUtils.isBlank(s)) {
return s;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// These characters are part of the query syntax and must be escaped
if (c == '\\' || c == '+' || c == '!'|| c == ','|| c == ';'
|| c == ':' || c == '^' || c == '[' || c == ']'|| c == ','
|| c == '{' || c == '}' || c == '~' || c == '?'|| c == '>'
|| c == '|' || c == '&' || c == ';' || c == '.'|| c == '<'
|| c == '$' || Character.isWhitespace(c)) {
sb.append('\\').append(c);
} else if (c == '(' || c == ')' || c == '/' || c == '*' || c == '\"') {
sb.append((byte) c);
}else {
sb.append(c);
}
}
String str = sb.toString();
if (str.contains("NUL")) {
str = str.replace("NUL", "0");
}
return str;
}