-1

我已经构建了一个侦听端口(6666)的 Java 服务器。现在,我需要使用 LDAP 浏览器(我使用 Softerra)连接到该服务器。连接已完成,但我必须知道何时有 LDAP 绑定/搜索,但我不知道该怎么做。

这是我的服务器的代码(如果不是很清楚/很好,请随时告诉我,我对 Java Prog 很陌生。):

package net.nantes.littleldap;
import java.net.*;
import java.io.*;

public class Serverside {

    public static void main(String[] args) {
        ServerSocket socketserver  ;
        Socket socket ;
        BufferedReader in;
        PrintWriter out;

        try {
            Authenticate auth = new Authenticate();
            socketserver = new ServerSocket(6666);
            System.out.println("Le serveur est à l'écoute du port "+socketserver.getLocalPort());
            auth.connect();
            socket = socketserver.accept(); 
            String inputLine = new String();
            in = new BufferedReader(
                        new InputStreamReader(
                        socket.getInputStream()));
                System.out.println("Connecté au serveur");
                while ((inputLine = in.readLine()) != null){
                    System.out.println(inputLine);
                out = new PrintWriter(socket.getOutputStream());
                out.println("Connection réussie");
                out.flush();
                }
                socket.close();
                socketserver.close();

        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

抱歉,消息是法语的,但这并不重要。我想也许我可以用 InputLine 做一些事情(当我打印它时,它会返回一些相对于 LDAP 的字符串,但我可能很难解析)。

那么,有什么想法吗?非常感谢 !

4

3 回答 3

1

我强烈建议您使用 JNDI 或可用的 LDAP SDK 之一。我们喜欢:https: //www.unboundid.com/products/ldap-sdk/ -jim

于 2012-09-12T12:47:16.383 回答
0

除了侦听端口之外,您的服务器还必须“理解”LDAP 协议。我使用 OpenDS LDAP SDK (http://www.middleware.vt.edu/pubs/opends-sdk-0.9.0/)。

代码是这样的

public class MyLdapServer 
implements ServerConnectionFactory<LDAPClientContext, Integer> {

private LDAPListener listener;

public void init() {
    try {
        listener = new LDAPListener(1389, this);
    } catch (IOException e) {
        logger.error("error opening LDAP listener", e);
    }
}

public void destroy() {
   listener.close();
}

@Override
public ServerConnection<Integer> handleAccept(LDAPClientContext context)
        throws ErrorResultException {
    if (logger.isDebugEnabled())
        logger.debug("ldap connection from: " + context.getPeerAddress());

    IncomingLdapConnection ilc = new IncomingLdapConnection(context);
    return ilc;
}

private static Logger logger = LoggerFactory.getLogger(MyLdapServer.class);

}

IncomingLdapConnection 允许您处理 LDAP 操作:

public class IncomingLdapConnection 
implements ServerConnection<Integer> {

    public void handleBind(Integer ctx, int version, BindRequest request,
        ResultHandler<? super BindResult> resultHandler,
        IntermediateResponseHandler intermediateResponseHandler)
throws UnsupportedOperationException {
    if (request.getAuthenticationType() != -128) {
        logger.warn("LDAP BIND: unsupported authentication type: " + request.getAuthenticationType());
        resultHandler.handleResult(Responses.newBindResult(ResultCode.AUTH_METHOD_NOT_SUPPORTED));
        return;
    }

    String bindName = request.getName();
    if (bindName.length() > 0) {
        if (request instanceof GenericBindRequest) {
            GenericBindRequest bindRequest = (GenericBindRequest)request;

            String userName = parseUidDn(bindName);
            if (userName == null) {
                // manche LDAP-Clients senden keine DN, sondern direkt den Namen
                userName = bindName;
            }

            String password = bindRequest.getAuthenticationValue().toString();

            logger.debug("LDAP BIND: non-anonymous bind, user = " + userName);
            anonymous = false;
        } else {
            logger.warn("LDAP BIND: non-anonymous bind, but unsupported request");
            resultHandler.handleResult(Responses.newBindResult(ResultCode.AUTH_METHOD_NOT_SUPPORTED));
            return;
        }
    } else {
        logger.debug("LDAP BIND: anonymous bind");
        anonymous = true;
    }

    boolean success = anonymous;
    if (!anonymous) {
        // authenticate user, set "success"
    }

    if (success)
        resultHandler.handleResult(Responses.newBindResult(ResultCode.SUCCESS));
    else
        resultHandler.handleResult(Responses.newBindResult(ResultCode.INVALID_CREDENTIALS));

    authenticated = success;
}

编辑:用于回答 LDAP 搜索请求的 OpenDS 代码

public void handleSearch(Integer ctx, SearchRequest request,
        SearchResultHandler responseHandler, IntermediateResponseHandler intermediateResponseHandler)
    throws UnsupportedOperationException {
    if (request.getScope() == SearchScope.BASE_OBJECT && request.getName().isRootDN()) {
        logger.debug("LDAP Search: BASE_OBJECT");
        responseHandler.handleEntry(Responses.newSearchResultEntry(rootEntry));
    } else {
        // do the search
        // parameters: request.getName(), request.getScope(), request.getFilter()
    }

    responseHandler.handleResult(Responses.newResult(ResultCode.SUCCESS));
}
于 2012-09-11T16:12:26.360 回答
0

查看UnboundID LDAP SDK和一些示例代码

编辑:

我不推荐使用 JNDI:

  • JNDI 使用不推荐使用的配置
  • JNDI存在软件缺陷
  • JNDI 不完全支持 LDAP 标准

也可以看看

于 2012-09-11T16:05:26.927 回答