Почему ldaptemplate(ldapcontextsource) использует базу вместо URL-адресов, предоставленных для подключения?

#java #spring #ldap #spring-ldap

Вопрос:

Я тестирую функцию аварийного переключения 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 amp;amp; 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