LDAP Connection/ Apache DS / Java



我想使用Apache DS和Java建立一个简单的LDAP连接,想学习和玩一点身份验证。然而,当我开始使用我的jar文件时,我总是收到这样的错误消息:

Setting up LDAP connection ... 
LDAPException(resultCode=91 (connect error), errorMessage='An error occurred while attempting to resolve address 'ldap://localhost:10389':  
UnknownHostException(Der angegebene Host ist unbekannt (ldap://localhost:10389)), ldapSDKVersion=6.0.0, revision=524c20f3bbcc0d83fb56b9e136a2fd3a7f60437d')

我的apache DS LDAP服务器如下所示:

在此处输入图像描述

我的Java代码:

package ldap.test;
import java.security.GeneralSecurityException;
import javax.net.SocketFactory;
import com.unboundid.ldap.sdk.BindRequest;
import com.unboundid.ldap.sdk.BindResult;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPSearchException;
import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;
import com.unboundid.ldap.sdk.SimpleBindRequest;
import com.unboundid.util.ssl.SSLUtil;
import com.unboundid.util.ssl.TrustAllTrustManager;
public final class App2 {
// hostname of the ldap instance
public static final String HOSTNAME = "ldap://localhost:10389";

// port of the ldap instance
public static final int PORT = 10389;

public static final void main(String[] args)
{
// lazy hack
if ( args.length != 4 ) {
System.out.println("One or more parameters are missing!");
System.out.println("java -jar App2.jar $cn $sn $employeenumber $password");
System.out.println("Example: java -jar App2.jar Max Mustermann 1 1");
System.exit(1);
}
// Use no key manager, and trust all certificates. This should not be used in non-trivial code!
SSLUtil sslUtil = new SSLUtil(null, new TrustAllTrustManager());
SocketFactory socketFactory;
LDAPConnection ldapConnection = null;
try {
// Create the socket factory that will be used to make a secure
// connection to the server.
socketFactory = sslUtil.createSSLSocketFactory();
System.out.print("Setting up LDAP connection ... ");
ldapConnection = new LDAPConnection(socketFactory, HOSTNAME, PORT);
System.out.println("done!");
}
catch ( LDAPException ldapException ) {
System.err.println(ldapException);
System.exit(ldapException.getResultCode().intValue());
}
catch ( GeneralSecurityException exception ) {
System.err.println(exception);
System.exit(1);
}
// LDAP bindrequest and actual bind for DN search
System.out.print("Search DN for user with employeeNumber: " + args[2] + " ... ");
BindRequest ldapBind = new SimpleBindRequest(args[0], args[1]);
try {
// bind with technical user and password and search for DN
ldapConnection.bind(ldapBind);
String employeeNumber = args[2];
String userPassword = args[3];
Filter ldapFilter = Filter.createANDFilter(Filter.createEqualityFilter("number", employeeNumber));
SearchRequest searchReq = new SearchRequest("ou=users,o=Beispiel", SearchScope.SUB, ldapFilter, "dn");
SearchResult searchResult;
String foundDN = "none";
try
{
searchResult = ldapConnection.search(searchReq);
System.out.println("done!");
for ( SearchResultEntry entry : searchResult.getSearchEntries() )
{
foundDN = entry.getDN();
}
}
catch ( LDAPSearchException lse )
{
System.out.println("... error!");
// The search failed for some reason
searchResult = lse.getSearchResult();
ResultCode resultCode = lse.getResultCode();
System.out.println("Resultcode: " + resultCode);
String errorMessageFromServer = lse.getDiagnosticMessage();
System.out.println("Error message from server: " + errorMessageFromServer);
}
// now check for the foundDN if the given password is correct
if ( !foundDN.equals("none") ) {
System.out.println("Found DN for user with EmployeeNumber: " + employeeNumber + " => " + foundDN);
System.out.println("Now checking if password for user is correct!");
BindRequest userBindReq = new SimpleBindRequest(foundDN, userPassword);
BindResult userBindRes = ldapConnection.bind(userBindReq);
System.out.println("Result: " + userBindRes);
}
else {
System.out.println("No DN found for user with EmployeeNumber: " + employeeNumber);
}
}
catch ( LDAPException ldapException ) {
System.err.println(ldapException);
System.exit(ldapException.getResultCode().intValue());
}
finally {
// Close ldap connection
ldapConnection.close();
}
}
}

不知道为什么我不能连接到服务器。。。

-----编辑--------

当我将HOSTNAME更改为localhost时,我会得到以下错误消息:

Setting up LDAP connection ... LDAPException(resultCode=91 (connect error),
errorMessage='An error occurred while attempting to connect to server localhost:10389:  IOException(LDAPException(resultCode=91 (connect error), 
errorMessage='An error occurred while attempting to establish a connection to server localhost/127.0.0.1:10389:  SSLException(Unsupported or unrecognized SSL message), 
ldapSDKVersion=6.0.0, revision=524c20f3bbcc0d83fb56b9e136a2fd3a7f60437d'))') 

好的是,他可以找到localhost,但他无法连接到服务器

我遇到了类似的问题,通过只使用两个参数("localhost",10389(调用LDAConnection构造函数解决了这个问题。你能检查一下这是否也解决了你的问题吗?

当然,你也应该删除";ldap//:";前缀也是!

相关内容

  • 没有找到相关文章

最新更新