LDAP查询数据——LDAP连接池 (JAVA)

 


前言

接上一个文章,因为上一个是直接连接的没有使用spring提供的JAR包,导致实际使用中总会有连接超时异常,测试给提了bug,刚开始准备自己写一个连接池,后来尝试了下算了,不重复造轮子了,自己写也不一定写的好,因此不得不去研究下LDAP连接池的

一、连接池是什么?

连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。

都应该使用过数据连接池,例如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());
    }
}

很多配置官网都有,我就是使用了默认的配置

官网地址:Spring LDAP Reference

3.实际使用

使用起来也挺简单的,就当作一个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;
    }


总结

query方法是连接池自带的!!!

 如果有幸看到文章并且帮助您了,帮忙点个赞,不白分享

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
根据提供的引用内容,建立LDAP连接池的步骤如下: 1. 配置LDAP连接池的上下文环境: ```java Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://ldap.example.com:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com"); env.put(Context.SECURITY_CREDENTIALS, "password"); ``` 2. 创建LDAP连接池对象: ```java DirContext ctx = new InitialDirContext(env); ``` 3. 配置连接池属性: ```java ctx.addToEnvironment(javax.naming.Context.PROVIDER_URL, "ldap://ldap.example.com:389"); ctx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, "simple"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com"); ctx.addToEnvironment(javax.naming.Context.SECURITY_CREDENTIALS, "password"); ctx.addToEnvironment(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "ssl"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "tls"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "ssl"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "ssl"); ``` 4. 创建连接池对象: ```java DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/dbpooling"); ``` 请注意,以上代码仅为示例,实际配置和使用LDAP连接池可能会因环境和需求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值