为什么 ldaptemplate(ldapcontextsource) 使用 base 而不是为连接提供的 URL?

问题描述

我正在测试 spring ldap 的故障转移功能

我的连接配置是以下 URL 和基本 DN

ldap:
  personDaoImpl:
    ldapConfigProperties:
      base: DC=exm,DC=example,DC=com
      ssl: ldaps://192.168.178.57:636 ldaps://192.168.178.50:636

我正在按如下方式创建 ldap 模板

    @Bean(name = "contextSource")
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        try {
            String url = null;
            
            Map<String,Object> baseEnvironmentProperties = new HashMap<String,Object>();
            baseEnvironmentProperties.put("com.sun.jndi.ldap.connect.timeout",personDaoImpl.getLdapConfigProperties().getConnectTimeout());
            baseEnvironmentProperties.put("com.sun.jndi.ldap.read.timeout",personDaoImpl.getLdapConfigProperties().getReadTimeout());
            if (personDaoImpl.getLdapConfigProperties().getConnectionType().toLowerCase() == "insecure") {
                url = personDaoImpl.getLdapConfigProperties().getUrl();
            } else {
                url = personDaoImpl.getLdapConfigProperties().getSsl();
                baseEnvironmentProperties.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
                baseEnvironmentProperties.put(Context.Security_AUTHENTICATION,"simple");
                baseEnvironmentProperties.put("java.naming.ldap.version","3");
                baseEnvironmentProperties.put(Context.REFERRAL,"follow");
                baseEnvironmentProperties.put("java.naming.ldap.factory.socket","com.rockwell.auth.configuration.CustomSSLSocketFactory");
                System.setProperty("com.sun.jndi.ldap.connect.pool","false");
            }

            String split[] = url.split("\\ ");
            contextSource.setUrls(split);
            contextSource.setReferral("follow");
            contextSource.setBase(personDaoImpl.getLdapConfigProperties().getBase());
            contextSource.setUserDn(personDaoImpl.getLdapConfigProperties().getUser());
            contextSource.setPassword(personDaoImpl.getLdapConfigProperties().getpassword());
            contextSource.setBaseEnvironmentProperties(baseEnvironmentProperties);
            contextSource.afterPropertiesSet();
        } catch (Exception e) {
            System.err.println((new StringBuilder(" LDAP Context Error ")).append(e.getMessage()).toString());
        }
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        LdapTemplate ldapTemplate = new LdapTemplate(contextSource());
        try {
            ldapTemplate.afterPropertiesSet();
            personDaoImpl.setLdapTemplate(ldapTemplate);
        } catch (Exception e) {
            e.printstacktrace();
        }
        return ldapTemplate;
    }

我使用 ldapTemplate 查询用户如下:

public User getUserBylogonName(String logon) {
        User user = null;
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass","User")).and(new EqualsFilter("sAMAccountName",logon));

        List<User> userList = ldapTemplate.search("",filter.toString(),getContextMapper(""));

        if(userList != null && userList.size() > 0){
            user = userList.get(0);
        }else{
            user = new User();
        }
        return user;
    }

当我尝试搜索用户时,我发现 ldapTemplate 使用基本 DN 进行连接,而不是提供的 URL:

org.springframework.ldap.PartialResultException: nested exception is javax.naming.PartialResultException [Root exception is javax.naming.CommunicationException: exm.example.com:636 [Root exception is java.net.UnkNownHostException: exm.example.com]]
        at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:216)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:385)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:328)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:629)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:570)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:530)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:562)
        at com.rockwell.ldap.ad.domain.Impl.UserRepoImpl.getUserBylogonName(UserRepoImpl.java:232)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodon(RepositoryFactorySupport.java:503)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:478)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
        at com.sun.proxy.$Proxy78.getUserBylogonName(UnkNown Source)
        at com.rockwell.ldap.ad.services.UserService.createuser(UserService.java:195)
        at com.rockwell.auth.controller.UserController.lambda$createuser$2(UserController.java:114)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: javax.naming.PartialResultException [Root exception is javax.naming.CommunicationException: exm.example.com:636 [Root exception is java.net.UnkNownHostException: exm.example.com]]
        at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreImpl(AbstractLdapNamingEnumeration.java:237)
        at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMore(AbstractLdapNamingEnumeration.java:189)
        at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:365)
        ... 28 more
Caused by: javax.naming.CommunicationException: exm.example.com:636 [Root exception is java.net.UnkNownHostException: exm.example.com]
        at com.sun.jndi.ldap.LdapReferralContext.<init>(LdapReferralContext.java:96)
        at com.sun.jndi.ldap.LdapReferralException.getReferralContext(LdapReferralException.java:150)
        at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreReferrals(AbstractLdapNamingEnumeration.java:325)
        at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreImpl(AbstractLdapNamingEnumeration.java:227)
        ... 30 more
Caused by: java.net.UnkNownHostException: exm.example.com
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
        at java.net.socksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.socket.connect(Socket.java:589)
        at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
        at sun.reflect.GeneratedMethodAccessor39.invoke(UnkNown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.sun.jndi.ldap.Connection.createSocket(Connection.java:311)
        at com.sun.jndi.ldap.Connection.<init>(Connection.java:203)
        at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:137)
        at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1614)
        at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2746)
        at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:319)
        at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:192)
        at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:151)
        at com.sun.jndi.url.ldap.ldapURLContextFactory.getobjectInstance(ldapURLContextFactory.java:52)
        at javax.naming.spi.NamingManager.getURLObject(NamingManager.java:601)
        at javax.naming.spi.NamingManager.processURL(NamingManager.java:381)
        at javax.naming.spi.NamingManager.processURLAddrs(NamingManager.java:361)
        at javax.naming.spi.NamingManager.getobjectInstance(NamingManager.java:333)
        at com.sun.jndi.ldap.LdapReferralContext.<init>(LdapReferralContext.java:119)
        ... 33 more
07:52:38.778 [http-nio-8090-exec-8] WARN  com.rockwell.auth.util.CustomExceptionHandler - org.springframework.ldap.PartialResultException

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)