UnboundID:如何封装processBindRequest返回值?

时间:2014-01-16 23:57:13

标签: java unboundid-ldap-sdk

我正在为我的数据库构建一个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)

我认为,它是由processBindRequest()返回null引起的。如何在我的进程中将我的数据库记录封装为LDAPMessage?

1 个答案:

答案 0 :(得分: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的响应,如果您想向客户端提供有关绑定失败原因的信息,您可以将其设置为在诊断消息元素中。