UnboundID:如何封装进程绑定请求返回值



我正在为我的数据库构建一个LDAP接口。当客户端请求 bind() 时,它将在数据库中搜索并检查它是否有效。

public class Main {
    LDAPListener listener ;
    Main() {}
    public static void main(String[] args) {
        Main main = new Main();
        int port = main.StartServer();
        try {
        LDAPConnection cn = new LDAPConnection("localhost",port);
            System.out.println("."+cn.isConnected()+" "+cn.getConnectedPort());
            cn.bind("uid=user,ou=People,dc=example,dc=com", "pass");
            cn.close();
            main.StopServer();
        } catch (Exception e){e.printStackTrace();
            main.StopServer();}
    }
    public int StartServer() {
        int listenPort = 0;
        RequestHandler requestHandler = new RequestHandler();
        LDAPListenerConfig config = new LDAPListenerConfig(listenPort, requestHandler);
        listener = new LDAPListener(config);
        try {
            listener.startListening();
            System.out.println(">port "+listener.getListenPort());          
        } catch (Exception e){System.out.println("e1> "+e.getMessage());}
        return listener.getListenPort();
    }
    public void StopServer(){
        System.out.println(">shutdown");
        listener.shutDown(true);
    }
}

然后,我修改LDAPListenerRequestHandler与数据库通信,获取记录作为返回值:

class RequestHandler extends LDAPListenerRequestHandler {
    @Override
    public LDAPMessage processBindRequest(int arg0, BindRequestProtocolOp arg1,
        List<Control> arg2) {
        String uid = arg1.getBindDN();
        String pass = arg1.getSimplePassword();
        System.out.println(">bind: "+ uid);
        // Database query: SELECT * FROM user WHERE username='uid' AND password='pass'
        // Get the record as return value
        return null;
    }
}

当我运行它时,我从绑定行收到错误消息:

LDAPException(resultCode=80 (other), errorMessage='An unexpected exception was thrown while attempting to process the requested operation:  NullPointerException(trace='run(LDAPListenerClientConnection.java:461)', revision=15579)', diagnosticMessage='An unexpected exception was thrown while attempting to process the requested operation:  NullPointerException(trace='run(LDAPListenerClientConnection.java:461)', revision=15579)')
    at com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:1881)
    at com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:1799)

我认为,这是由返回 null 的进程绑定请求() 引起的。如何在该过程中将我的数据库记录封装为 LDAPMessage?

您是正确的,processBindRequest 方法必须返回非空响应。

如果绑定成功(用户存在、被允许进行身份验证并提供了正确的凭据),则可以使用如下代码创建成功的响应:

 @Override()
 public LDAPMessage processBindRequest(final int messageID,
                                       final BindRequestProtocolOp request,
                                       final List<Control> controls)
 {
   return new LDAPMessage(messageID, 
        new BindResponseProtocolOp(ResultCode.SUCCESS_INT_VALUE, 
             null,  // No matched DN is needed
             null,  // No diagnostic message is needed
             null,  // No referral URLs are needed
             null), // No server SASL credentials are needed
        Collections.<Control>emptyList()); // Add empty list to return
 }

如果身份验证不成功,则可能应返回结果代码为 INVALID_CREDENTIALS 而不是 SUCCESS 的响应,如果要向客户端提供一条消息,其中包含有关绑定失败原因的信息,则可以将其放在诊断消息元素中。

最新更新