deathstr0ke
deathstr0ke

Reputation: 11

Java connection to LDAP server

I am struggling to make a connection to a LDAP server using java. I've tried JNDI and failed, I read that unboundid sdk is probably the way to go.. but still having some issues :(

End Goal: I want to be able to connect to the LDAP server using Kerberos authentication. Once I can establish the connection, I can start looking at the code to execute a search :)

Now I believe I am on the right track, but I'll be honest.. after searching for over two weeks and reading multiple forums.. I'm beginning to lose faith in my ability to establish a connection.

Below is the error I am currently encountering..

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/unboundid/ldap/sd
k/LDAPException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.unboundid.ldap.sdk.LDAPExceptio
n
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

I've added the unboundid-ldapsdk.jar to Project Structures > Libraries

I confirmed that unboundid-ldapsdk shows under under Project Structures > Module > Dependencies.

.

below is my code:

import com.unboundid.ldap.sdk.*;

public class LdapConnectionCreation {
    public static void main(String[] args) {


        GSSAPIBindRequestProperties gssapiProperties = new GSSAPIBindRequestProperties("username", (byte[]) null);
        gssapiProperties.setKDCAddress("server.domain.ca");
        gssapiProperties.setRealm("domain.ca");

        LDAPConnection connection = null;
        try {
            connection = new LDAPConnection("server.domain.ca", 3268);
        }
        catch (LDAPException e) {
            e.printStackTrace();
        }

        GSSAPIBindRequest bindRequest = null;
        try {
            bindRequest = new GSSAPIBindRequest(gssapiProperties);
        }
        catch (LDAPException e) {
            e.printStackTrace();
        }

        BindResult bindResult;
        System.out.println("testing bind");


        try {
            bindResult = connection.bind(bindRequest);
            System.out.println("bind successful");
        }
        catch (LDAPException e) {
            System.out.println("bind failed");
            bindResult = new BindResult(e.toLDAPResult());
            ResultCode resultCode = e.getResultCode();
            String errorMessageFromServer = e.getDiagnosticMessage();
            System.out.println(resultCode);
            System.out.println(errorMessageFromServer);
            System.out.println("---------trace---------");
            e.printStackTrace();
        }

    }
}

*NOTE: I have the proper username and server names in the code, just not included in the code above :)

I am also fairly new to java programming, so any help and tips would be greatly appreciated!

Upvotes: 1

Views: 736

Answers (0)

Related Questions