2014-01-16 44 views
0

我正在爲我的數據庫構建一個LDAP接口。當一個客戶端請求bind()時,它將在數據庫中搜索並檢查它是否有效。UnboundID:如何封裝processBindRequest返回值?

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進程?

回答

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的響應,如果你想向客戶端提供一個消息,告訴客戶端爲什麼綁定失敗,你可以把它放在診斷消息元素中。

+0

感謝您的回答。編輯processBindRequest()後我仍然得到相同的錯誤ResultCode = 80 – vladschwartz

+0

我建議您啓用調試日誌記錄(如com.unboundid.util.Debug類中所述),看看哪裏和什麼異常被拋出。由於您沒有提供完整的代碼,因此很難知道具體問題是什麼。 –