prokovalya
prokovalya

Reputation: 11

Spring ldap: creating connection

The following is my code:

private DirContext createContext() {

    Hashtable env = new Hashtable();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.springframework.ldap.core.support.LdapContextSource");
    env.put(Context.PROVIDER_URL, "ldap://FAKESERV.fakedom:389/");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "fakeuser@fakedom");
    env.put(Context.SECURITY_CREDENTIALS, "1qaz!QAZ");

    try {
        return new InitialDirContext(env);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}

It works fine, but I should use the xml config.

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="url" value="ldap://FAKESERV.fakedom:389/"/>
    <property name="base" value="ou=FAKESERV.fakedom"/>
    <property name="userDn" value="uid=fakeuser@fakedom"/>
    <property name="password" value="1qaz!QAZ"/>
    <property name="contextFactory" value="com.sun.jndi.ldap.LdapCtxFactory"/>
</bean>

Using ldapTemplate.getContextSource().getReadOnlyContext() I have 525 error - "User not found". How to modify this xml?

server: FAKESERV
fomain: fakedom
user: fakeuser
url: ldap://FAKESERV.fakedom:389/

All properties in Active Directory is default.

Also, how do I execute ldap request for searching some user?

UPD: now I used impl. for ActiveDirectory:

<bean id="authenticationToken" class="org.springframework.security.authentication.UsernamePasswordAuthenticationToken">
        <constructor-arg value="fakeuser@fakedom" />
        <constructor-arg value="1qaz!QAZ" />
    </bean>
    <bean id="authenticationProvider"
         class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
        <constructor-arg value="fakedom" />
        <constructor-arg value="ldap://FAKESERV.fakedom:389/" />
    </bean>

it's work fine, thank you.

now I trying to send ldap-request to server...

Upvotes: 1

Views: 3249

Answers (1)

ssedano
ssedano

Reputation: 8432

Spring security now offers connection to LDAP/AD. But one thing you can try is to set:

com.sun.jndi.ldap.LdapCtxFactory

as Context.INITIAL_CONTEXT_FACTORY

And InitialLdapContext as context does it connect?

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://FAKESERV.fakedom:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "fakeuser@fakedom");
env.put(Context.SECURITY_CREDENTIALS, "1qaz!QAZ");

try {
    return new InitialLdapContext(env, null);
} catch (NamingException e) {
    throw new RuntimeException(e);
}

If this work then it is a problem with the configuration.

Good reading with examples.

Upvotes: 1

Related Questions