Java中的Ldap套接字示例
我正在用java创建Ldap套接字,类似于Java中的Ldap套接字示例,java,sockets,ldap,Java,Sockets,Ldap,我正在用java创建Ldap套接字,类似于 我希望ldap套接字能够接收ldap调用并打印它们,然后在消息中将虚拟ldap响应返回给客户端。 它似乎能够连接到LDAP客户端,但不向其接收或发送消息。 LDAP服务器:- public class Server { private Socket socket = null; private ServerSocket server = null; private DataInputStream
我希望ldap套接字能够接收ldap调用并打印它们,然后在消息中将虚拟ldap响应返回给客户端。
它似乎能够连接到LDAP客户端,但不向其接收或发送消息。
LDAP服务器:-
public class Server
{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
public Server(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
if (s.isEmpty()) {
break;
}
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Server server = new Server(10389);
}
}
LDAP客户端
public class App
{
DirContext connection;
public static void main( String[] args ) throws NamingException {
App app = new App();
app.createConnection();
app.addUser();
}
private void createConnection() {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
properties.put(Context.SECURITY_PRINCIPAL, "uid=admin, ou=system");
properties.put(Context.SECURITY_CREDENTIALS, "secret");
try {
connection = new InitialDirContext(properties);
System.out.println( "Hello World! " + connection);
} catch (NamingException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
public void addUser(){
Attributes attributes = new BasicAttributes();
Attribute attribute = new BasicAttribute("objectClass");
attribute.add("inetOrgPerson");
attributes.put(attribute);
attributes.put("sn","c");
try {
connection.createSubcontext("cn=k,ou=users,ou=system", attributes);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
输出:-服务器已启动
正在等待客户端…
客户接受了这是一项艰巨的任务。您必须实现LDAP协议,其中包括ASN.1 BER解析:然后必须在服务器中实现LDAP语义。太宽泛了,对你来说可能完全不可行。我投票结束这个问题,因为它太宽泛了,无法在这里回答。