首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnboundID :如何封装processBindRequest返回值?

UnboundID :如何封装processBindRequest返回值?
EN

Stack Overflow用户
提问于 2014-01-17 07:57:13
回答 1查看 397关注 0票数 0

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

代码语言:javascript
复制
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来与数据库通信,获取记录作为返回值:

代码语言:javascript
复制
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;
    }
}

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

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

发布于 2014-01-17 15:23:32

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

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

代码语言:javascript
复制
 @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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21175387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档