Implementation of timeout in LDAP

血红的双手。 提交于 2019-12-12 10:01:21

问题


I have been handling an application in which we are using LDAP to fetch user details. Sometimes it will take more time to fetch user details. I want to implement time out on methods that fetch details so that we can avoid hanging transactions in server in worst case.

Here we are using LdapUtil class in which we have configured LdapTemplate class to fetch the required details.

How can we implement timeout on LDAP methods? (In this case ldapTemplate.search(...) methods)

public class LdapUtil {

    @Autowired(required = true)
    @Qualifier(value = "ldapTemplateApp")
    LdapTemplate ldapTemplate;

    public Set < ProductGroup > findProducts(String UserId) {
        final Set < ProductGroup > products = newHashSet();
        // Lookup the user         
        String usrFilter = String.format(USERID_FILTER, globalUserId);

        ldapTemplate.search("ou=Members", usrFilter, // note this line
        new NameClassPairCallbackHandler() {

            public void handleNameClassPair(NameClassPair nameClassPair) {
                SearchResult result = (SearchResult) nameClassPair;
                String user = result.getNameInNamespace();

                String GrpFilter = String.format(GROUP_FILTER, user);

                List < String > zonePrefixes = ldapTemplate.search("Zones", GrpFilter, // note this line
                new AttributesMapper() {
                    public Object mapFromAttributes(Attributes attributes) throws NamingException {
                        return substringBeforeLast((String) attributes.get("cn").get(), "-") + "-";
                    }
                });

            }
        });

        products.remove(null);
        return newHashSet(products);
    }
}   

We have one LDAP.xml in which ldapTemplete is configured

<beans xmlns="------">
<!-- LDAP -->
<bean id="contextSourceApp" class="org.springframework.ldap.pool.factory.PoolingContextSource">
<property name="contextSource" ref="contextSourceTargetApp" />
<property name="dirContextValidator">
<bean id="dirContextValidator"   
class="org.springframework.ldap.pool.validation.DefaultDirContextValidator"/>
</property>         
<property name="testOnBorrow" value="true" />
</bean>
<bean id="contextSourceTargetApp" class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="url" value="${ldap.url}" />
    <property name="base" value="${ldap.base.}" />
    <property name="userDn" value="${ldap.user}" />
    <property name="password" value="${ldap.password}" />
    <property name="pooled" value="false" />
</bean>

<bean id="ldapTemplateApp" class="org.springframework.ldap.core.LdapTemplate">
    <constructor-arg ref="contextSourceApp" />
</bean>

I have few queries:

  1. How can we implement the TIMEOUT for LDAP methods and how to configure it?(In which class of LDAP framework timeout settings will be there)

  2. Is there any way to configure them in xml file i.e. LDAP.xml(in this case)?


回答1:


I found a solution. I have added the following property in my ldap.xml file. So far it worked for me.

<bean id="contextSourceTargetApp" 
      class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="baseEnvironmentProperties">
        <map>
            <entry key="com.sun.jndi.ldap.connect.timeout" value="5000" />          
        </map>  
    </property>
</bean>

Please post any other solution if you have any idea about LDAP timeout implementation.




回答2:


For ActiveDirectoryLdapAuthenticationProvider the solution with ldap.xml file did not work for me. Instead I added a jndi.properties file to the classpath with the following content:

com.sun.jndi.ldap.connect.timeout=500


来源:https://stackoverflow.com/questions/23887669/implementation-of-timeout-in-ldap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!