0

我正在为我的数据库构建一个 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 的 processBindRequest() 引起的。如何在该过程中将我的数据库记录封装为 LDAPMessage?

4

1 回答 1

0

你是正确的 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 的响应,并且如果您想向客户端提供有关绑定失败原因的信息,您可以将其放入诊断中消息元素。

于 2014-01-17T07:23:32.027 回答