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

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 ne = ctx.search(dn, filter, ctrls);

....

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

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

附言

private LdapContext createLdapCtx(Object login, Object password) throws NamingException {

Hashtable props = new Hashtable();

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

}

解决方法:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值