2011-08-10 52 views
1

下面是我的代码:春LDAP:创建连接

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, "[email protected]"); 
    env.put(Context.SECURITY_CREDENTIALS, "1qaz!QAZ"); 

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

它工作正常,但我应该使用XML配置。

<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="[email protected]"/> 
    <property name="password" value="1qaz!QAZ"/> 
    <property name="contextFactory" value="com.sun.jndi.ldap.LdapCtxFactory"/> 
</bean> 

使用ldapTemplate.getContextSource().getReadOnlyContext() 我有525错误 - “用户未找到”。如何修改这个XML?

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

Active Directory中的所有属性都是默认值。

另外,如何执行ldap请求搜索某些用户?

UPD: 现在我使用了impl。对于ActiveDirectory:

<bean id="authenticationToken" class="org.springframework.security.authentication.UsernamePasswordAuthenticationToken"> 
     <constructor-arg value="[email protected]" /> 
     <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> 

它工作正常,谢谢。

现在我要发送的LDAP请求到服务器...

回答

1

春季安全现在提供连接LDAP/AD。但有一两件事你可以尝试是设置:

com.sun.jndi.ldap.LdapCtxFactory 

Context.INITIAL_CONTEXT_FACTORY

而且InitialLdapContext作为上下文它连接?

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, "[email protected]"); 
env.put(Context.SECURITY_CREDENTIALS, "1qaz!QAZ"); 

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

如果这工作,那么它是一个配置问题。

Good reading with examples