如何使用 UnboundID 获取 DN 和密码

2022-01-17 00:00:00 ldap openldap java unboundid-ldap-sdk

我需要一些关于 UnboundID 的帮助.我听说这是个不错的选择,但我不太习惯.

I need some help concerning UnboundID. I heard it was a great choice but I'm not really used to it.

所以我需要创建一个 LDAP 监听器.在这个监听器上,我应该能够捕获绑定请求(例如来自 ldap 浏览器).我想知道如何获取 DN 和密码.这是我的 LDAP 侦听器代码:

So I need to make a LDAP listener. On this listener, i should be able to catch bind request (from a ldap browser for example). I wonder how to get the DN and the password. Here is my code for the LDAP listener:

    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 文档,但找不到任何我需要的简单示例.

I read a lot of UnboundID documentation, but i can't find any simple example of what I need.

另外,我不太确定 CannedResponseRequestHandler 的实用性.满足我的需要,够吗?

Also, i'm not really sure of the utility of CannedResponseRequestHandler. For what i need, is it enough ?

另一个问题:我不确定,但我感觉我的服务器没有在监听,或者我什么也没有捕捉到(当我使用 ldap 浏览器连接时,什么也没发生).有什么想法/建议吗?

An other question: I'm not sure, but I have the feeling that my server is not listening OR i don't catch anything (when I connect with a ldap Browser, nothing happened). Any Idea / Suggestion ?

谢谢,祝你有美好的一天!

Thanks and have a nice day !

感谢 xhochy,我能够获取密码和用户名.正如他所说,我将 LDAPListenerRequestyHandler 子类化为覆盖,首先是 newInstance,然后是 ProcessBindRequest.这是代码(它绝对不完美,它仍然是一个开始).

EDIT : Thanks to xhochy, I was able to catch the password and the username. As he said, I subclassed LDAPListenerRequestyHandler to override, first, newInstance then ProcessBindRequest. Here is the code (it's absolutely not perfect and it's still a beginning).

公共类 MyConnection {

public class 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的子类:

Then the subclass of 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;
}

}

再次感谢!

推荐答案

你应该继承 LDAPListenerRequestHandler 并实现 processBindRequest.您要查找的所有信息都包含在 BindRequestProtocolOp(processBindRequest 的第二个参数)中.为所有其他抽象方法添加一个空实现.

You should subclass LDAPListenerRequestHandler and implement processBindRequest. All the information you are looking for is included in BindRequestProtocolOp (second argument of processBindRequest). Add an empty implementation for all other abstract methods.

如果 request 是您的 BindRequestProtocolOp 实例,那么您可以通过以下方式获取信息:

If request is your BindRequestProtocolOp instance then you get your information via:

String username = request.getBindDN();
ByteString password = request.getSimplePassword();

相关文章