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