【问题标题】:Implementation of timeout in LDAPLDAP中超时的实现
【发布时间】:2014-07-16 06:18:40
【问题描述】:

我一直在处理我们使用 LDAP 获取用户详细信息的应用程序。有时需要更多时间来获取用户详细信息。我想在获取详细信息的方法上实现超时,这样我们就可以避免在最坏的情况下挂起服务器中的事务。

这里我们使用LdapUtil 类,我们在其中配置了LdapTemplate 类来获取所需的详细信息。

我们如何在 LDAP 方法上实现超时? (在本例中为 ldapTemplate.search(...) 方法)

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);
    }
}   

我们有一个 LDAP.xml,其中配置了 ldapTemplete

<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>

我有几个问题:

  1. 我们如何为 LDAP 方法实现 TIMEOUT 以及如何配置它?(在哪类 LDAP 框架中会有超时设置)

  2. 有没有办法在 xml 文件中配置它们,即 LDAP.xml(在这种情况下)?

【问题讨论】:

    标签: java spring ldap spring-ldap ldapconnection


    【解决方案1】:

    对于ActiveDirectoryLdapAuthenticationProvider,带有 ldap.xml 文件的解决方案对我不起作用。相反,我在类路径中添加了一个 jndi.properties 文件,其内容如下:

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

    【讨论】:

      【解决方案2】:

      我找到了解决方案。 我在 ldap.xml 文件中添加了以下属性。到目前为止,它对我有用。

      <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>
      

      如果您对 LDAP 超时实施有任何想法,请发布任何其他解决方案。

      【讨论】:

        猜你喜欢
        • 2020-04-30
        • 2011-10-04
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多