简体   繁体   English

Spring ldap:创建连接

[英]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.它工作正常,但我应该使用 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="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".使用ldapTemplate.getContextSource().getReadOnlyContext()我有 525 错误 - “找不到用户”。 How to modify this xml?如何修改这个 xml?

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

All properties in Active Directory is default. Active Directory 中的所有属性都是默认的。

Also, how do I execute ldap request for searching some user?另外,如何执行 ldap 请求以搜索某些用户?

UPD: now I used impl. UPD:现在我使用 impl。 for ActiveDirectory:对于 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...现在我试图将 ldap-request 发送到服务器......

Spring security now offers connection to LDAP/AD. Spring 安全现在提供与 LDAP/AD 的连接。 But one thing you can try is to set:但是您可以尝试的一件事是设置:

com.sun.jndi.ldap.LdapCtxFactory

as Context.INITIAL_CONTEXT_FACTORY作为Context.INITIAL_CONTEXT_FACTORY

And InitialLdapContext as context does it connect?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, "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 . 很好的阅读示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM