[英]How to check whether current LDAP server is up or down before doing an authentication
我想在进行身份验证之前检查当前LDAP服务器是否已启动。 我正在使用UnboundID LDAP SDK。 有什么可行的方法吗?
检查端口是否已启动并正在运行。 是纯Java语言。 不需要SSL工厂或SSL上下文或凭据。 如果程序包被防火墙丢弃,则超时时间(以毫秒为单位)。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public static boolean isPortReachable(String host, int port, int timeOutMS) {
try (Socket serverSocket = new Socket();) {
serverSocket.connect(new InetSocketAddress(host, port), timeOutMS);
return true;
} catch (final IOException e) { /* Ignore, Port not reachable */ }
return false;
}
LDAP或LDAPS的用法:
if (isPortReachable(hostname, 389, 200)) { ... }
if (isPortReachable(hostname, 636, 200)) { ... }
有一个isConnected()方法:
public boolean isConnected()
Indicates whether this connection is currently established.
Returns:
true if this connection is currently established, or false if it is not.
因此,一个简单的示例如下所示:
LDAPConnection ldc = new LDAPConnection()
ldc.connect(...);
if (ldc.isConnected())
{
do good stuff
}
else
{
getLDAPConnection(...);
}
应该做到的。
检验任何资源是否可用的最佳方法就是简单地尝试在应用程序的正常过程中使用它,并应对出现的错误。
否则,您将尝试预测未来。
我们可以使用SSUtil来做到这一点。 此处不需要经过身份验证的条目,我们可以在进行任何身份验证之前检查服务器是否已启动。
SSLUtil su = new SSLUtil(new TrustAllTrustManager());
SSLSocketFactory sf = su.createSSLSocketFactory();
LDAPConnection connection = new LDAPConnection(sf,"localhost", 10636);
寻找这种解决方案的利弊。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.