spring-boot-starter-data-ldap连接ldap服务器测试功能

前言

因为项目要根据租户不同连接不同ldap服务器,要给租户管理员提供一个ldap配置页面,并提供测试连接ldap服务器测试功能。

代码

spring-boot-starter-data-ldap是直接在application文件中配置即可
(私人信息用**表示了)

  ldap:
    url: ldap://ldap.**.**:389
    base: DC=**,DC=**
    username: ***
    password: *****
    referral: follow

提供测试功能,连接多个ldap服务器,sysConfigParam页面配置信息和application配置一致。

LdapContextSource ldapContextSource = new LdapContextSource();
        ldapContextSource.setBase(sysConfigParam.getBase());
        ldapContextSource.setUrl(sysConfigParam.getUrl());
        ldapContextSource.setPassword(sysConfigParam.getPassword());
        ldapContextSource.setUserDn(sysConfigParam.getUsername());
        ldapContextSource.setReferral(sysConfigParam.getReferral());
        ldapContextSource.afterPropertiesSet();
        DirContext ctx = null;
        try {
            // Ldap link
            ctx = ldapContextSource.getReadOnlyContext();
            System.out.println("[auth ldap linked] InitialDirContext success");

        } catch (Exception e) {
            throw new ServiceException(SysLdapConfigExceptionEnum.DATA_SOURCE_NOT_EXIST);
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (NamingException e) {
                    throw new ServiceException(SysLdapConfigExceptionEnum.DATA_SOURCE_NOT_EXIST);
                }
            }
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容