ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-在Ldap搜索上设置方法超时

2019-11-19 02:01:17  阅读:722  来源: 互联网

标签:timeout nonblocking blocking java


private Authentication authenticateUserPassword(UsernamePasswordAuthenticationToken token) throws NamingException {
    Object login = login(token);
    LOGGER.debug("Starting authentication login='{}'", login);
    Object password = token.getCredentials();

    LdapContext ctx = createLdapCtx(login, password);
    SearchControls ctrls = createSearchControls();
    String filter = String.format(this.filter, login);

    NamingEnumeration<SearchResult> ne = ctx.search(dn, filter, ctrls);
    ....

我有以下登录用户的方法.它取决于LDAP.有时它挂在最后一行.我不知道为什么有时会在性能测试中进行复制.

有没有办法等待一段时间,如果方法没有响应-返回一些预定义的值?

附言

private LdapContext createLdapCtx(Object login, Object password) throws NamingException {
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(Context.INITIAL_CONTEXT_FACTORY, factory);
    props.put(Context.PROVIDER_URL, url);
    props.put(Context.SECURITY_AUTHENTICATION, "simple");
    props.put(Context.SECURITY_PRINCIPAL, String.format(domain, login));
    props.put(Context.SECURITY_CREDENTIALS, password.toString());

    return new InitialLdapContext(props, null);
}

解决方法:

您可以设置一个time-out for all Ldap operations

The new environment property: com.sun.jndi.ldap.read.timeout can be used to specify the read timeout for an LDAP operation. The value of this property is the string representation of an integer representing the read timeout in milliseconds for LDAP operations.

这样,您只需要更新createLdapCtx方法即可将该环境变量指定为您选择的值:

props.put("com.sun.jndi.ldap.read.timeout", "1000"); // 1 second of timeout here

如果服务器在1秒钟内没有响应,这将导致LDAP服务提供者中止读取尝试.如果达到超时,将抛出NamingException.

请注意,从this Stack Overflow post开始,您不能为此使用SearchControls.setTimeLimit方法,因为此参数不适用于读取超时.

标签:timeout,nonblocking,blocking,java
来源: https://codeday.me/bug/20191119/2032628.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有