在Java中使用LDAPConnection和端口636

时间:2020-06-12 16:43:59

标签: java ldap

我正在尝试获取应用程序的LDAP连接,以使用安全端口636而不是389。以下代码与端口389完美配合,但与636一起使用389引发了异常。

线程“ main”中的异常LDAPException(结果代码= 81(服务器关闭),errorMessage ='在等待对绑定请求的响应SimpleBindRequest(dn =时,已关闭与服务器nlbldap.company_name.co.uk:636的连接) 'jdoe@company_name.co.uk'):尝试从服务器读取响应时发生I / O错误:SocketException(message ='Connection reset',trace ='read(SocketInputStream.java:186)/ read( SocketInputStream.java:140)/ fill(BufferedInputStream.java:252)/ read(BufferedInputStream.java:271)/ read(ASN1StreamReader.java:3292)/ readType(ASN1StreamReader.java:329)/ beginSequence(ASN1StreamReader.java:912) )/ readLDAPResponseFrom(LDAPMessage.java:1146)/运行(LDAPConnectionReader.java:251)',版本= 24950)') 在com.unboundid.ldap.sdk.SimpleBindRequest.handleResponse(SimpleBindRequest.java:723) 在com.unboundid.ldap.sdk.SimpleBindRequest.process(SimpleBindRequest.java:575) 在com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:2154) 在com.unboundid.ldap.sdk.LDAPConnection。(LDAPConnection.java:670) 在com.unboundid.ldap.sdk.LDAPConnection。(LDAPConnection.java:563) 在LdapsMain1.LdapSoton.connect(LdapSoton.java:39) 在LdapsMain1.Main.main(Main.java:26)

package mywork.classes;

import java.util.List;
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.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;

public class Ldapcompany_name {

    static boolean connect(String username, String password) throws LDAPException 
    {                
        String baseDN = "ou=user,dc=company_name,dc=co,dc=uk";
        String filter = "(&(cn="+username+")(objectClass=user))";
        boolean hasRights = false;
        LDAPConnection connection = new LDAPConnection("nlbldap.company_name.co.uk", 389, username+"@company_name.co.uk", password);
        List<SearchResultEntry> results = getResults(connection, baseDN, filter);
        String msg = "results.size() = " + results.size();
        for(SearchResultEntry entry : results)
        {
            for(String v : entry.getAttributeValues("memberof")) 
            {
                if(v.contains("ISSWA-SIS-Web"))
                {
                    hasRights = true;
                }
            }
        }
        return hasRights;
    }
    private static List<SearchResultEntry> getResults(LDAPConnection connection, String baseDN, String filter) throws LDAPSearchException, LDAPException 
    {
        SearchResult searchResult;
        Filter filter2 = Filter.createEqualityFilter("ou", "*");
        SearchRequest searchRequest2 =  new SearchRequest("dc=company_name,dc=co,dc=uk", SearchScope.SUB, filter,"cn", "mail","memberOf");
        if (connection.isConnected()) 
        {
            searchResult = connection.search(searchRequest2);
            for (SearchResultEntry entry : searchResult.getSearchEntries())
            {
                  String name = entry.getAttributeValue("cn");
                  Object mail = entry.getAttributeValues("memberOf");
                  System.out.println(name + " " + mail);
            }
            return searchResult.getSearchEntries();
        }
        return null;
    }
}

这是堆栈跟踪

Exception in thread "main" LDAPException(resultCode=81 (server down), errorMessage='The connection to server nlbldap.company_name.co.uk:636 was closed while waiting for a response to a bind request SimpleBindRequest(dn='jdoe@company_name.co.uk'):  An I/O error occurred while trying to read the response from the server:  SocketException(message='Connection reset', trace='read(SocketInputStream.java:186) / read(SocketInputStream.java:140) / fill(BufferedInputStream.java:252) / read(BufferedInputStream.java:271) / read(ASN1StreamReader.java:992) / readType(ASN1StreamReader.java:329) / beginSequence(ASN1StreamReader.java:912) / readLDAPResponseFrom(LDAPMessage.java:1146) / run(LDAPConnectionReader.java:251)', revision=24950)')
    at com.unboundid.ldap.sdk.SimpleBindRequest.handleResponse(SimpleBindRequest.java:723)
    at com.unboundid.ldap.sdk.SimpleBindRequest.process(SimpleBindRequest.java:575)
    at com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:2154)
    at com.unboundid.ldap.sdk.LDAPConnection.<init>(LDAPConnection.java:670)
    at com.unboundid.ldap.sdk.LDAPConnection.<init>(LDAPConnection.java:563)
    at LdapsMain1.LdapSoton.connect(LdapSoton.java:39)
    at LdapsMain1.Main.main(Main.java:26)

2 个答案:

答案 0 :(得分:1)

如果不知道LDAP Result code,就很难确定。

但是我想您没有向连接对象提供任何TLS参数。

查看以下示例源代码:https://bitbucket.org/jwilleke/examples/src/master/Examples-JNDI/src/com/willeke/samples/ldap/jndi/ADConnection.java?at=master

答案 1 :(得分:0)

添加以下代码即可使该功能与端口636一起使用

SSLUtil sslUtil = new SSLUtil(null, new TrustAllTrustManager());
SSLSocketFactory socketFactory = sslUtil.createSSLSocketFactory();
LDAPConnection connection = new LDAPConnection(socketFactory, "nlbldap.company_name.com", 636);