【发布时间】: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>
我有几个问题:
我们如何为 LDAP 方法实现 TIMEOUT 以及如何配置它?(在哪类 LDAP 框架中会有超时设置)
有没有办法在 xml 文件中配置它们,即 LDAP.xml(在这种情况下)?
【问题讨论】:
标签: java spring ldap spring-ldap ldapconnection