我需要一些关于 UnboundID 的帮助。我听说这是一个不错的选择,但我不太习惯。
所以我需要做一个 LDAP 监听器。在这个监听器上,我应该能够捕获绑定请求(例如来自 ldap 浏览器)。我想知道如何获取 DN 和密码。这是我的 LDAP 侦听器代码:
public ResultCode CreateLdapServer () throws LDAPException {
CannedResponseRequestHandler requestHandler = new CannedResponseRequestHandler();
LDAPListenerConfig config =
new LDAPListenerConfig(4243, requestHandler);
try
{
config.setListenAddress(
InetAddress.getByName("localhost"));
}
catch (final Exception e)
{
System.err.println("Unable to create the listen server.");
return ResultCode.PARAM_ERROR;
}
listener = new LDAPListener(config);
try
{
listener.startListening();
System.out.println("Serveur is listening ...");
}
catch (final Exception e)
{
System.err.println("Unable to start listening.");
return ResultCode.LOCAL_ERROR;
}
return ResultCode.SUCCESS;
}
public static void main(String[] args) throws LDAPException {
MyConnection connect = new MyConnection();
connect.CreateLdapServer();
}
我阅读了很多 UnboundID 文档,但找不到任何我需要的简单示例。
另外,我不太确定 CannedResponseRequestHandler 的实用性。对于我所需要的,够了吗?
另一个问题:我不确定,但我感觉我的服务器没有在听或者我什么都没有(当我连接到 ldap 浏览器时,什么也没发生)。任何想法/建议?
感谢,并有一个愉快的一天 !
编辑:多亏了 xhochy,我才能够捕捉到密码和用户名。正如他所说,我将 LDAPListenerRequestyHandler 子类化为覆盖,首先是 newInstance,然后是 ProcessBindRequest。这是代码(它绝对不完美,它仍然是一个开始)。
公共类 MyConnection {
private LDAPListener listener;
public MyConnection(){
}
public ResultCode CreateLdapServer() throws LDAPException {
MyLDAPListenerRequestHandler requestHandler = new MyLDAPListenerRequestHandler();
LDAPListenerConfig config =
new LDAPListenerConfig(4243, requestHandler);
try
{
config.setListenAddress(
InetAddress.getByName("localhost"));
}
catch (final Exception e)
{
System.err.println("Unable to create the listen server.");
return ResultCode.PARAM_ERROR;
}
listener = new LDAPListener(config);
try
{
listener.startListening();
System.out.println("Serveur is listening ...");
}
catch (IOException e)
{
System.err.println("Unable to start listening.");
return ResultCode.LOCAL_ERROR;
}
return ResultCode.SUCCESS;
}
public static void main(String[] args) throws LDAPException {
MyConnection connect = new MyConnection();
connect.CreateLdapServer();
}
}
然后是LDAPListenerRequestHandler的子类:
public class MyLDAPListenerRequestHandler extends LDAPListenerRequestHandler {
@Override
public LDAPListenerRequestHandler newInstance(
LDAPListenerClientConnection arg0) throws LDAPException {
System.out.println("New Instance.");
LDAPConnectionOptions option = new LDAPConnectionOptions();
LDAPConnection connection = new LDAPConnection(option, "yourIPadress", yourport);
System.out.println("Connected to : " + connection.getConnectedAddress()+ " " + connection.getConnectedPort());
return this;
}
@Override
public LDAPMessage processBindRequest(int arg0, BindRequestProtocolOp arg1,
List<Control> arg2) {
System.out.println(arg1.getBindDN());
System.out.println(arg1.getSimplePassword());
return null;
}
}
再次感谢 !