gpt4 book ai didi

java - Spring LDAP : creating connection

转载 作者:太空宇宙 更新时间:2023-11-04 08:33:13 31 4
gpt4 key购买 nike

以下是我的代码:

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);
}
}

它工作正常,但我应该使用 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>

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

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

Active Directory 中的所有属性均为默认属性。

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

更新:现在我用了 impl。对于 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>

一切正常,谢谢。

现在我尝试将 ldap 请求发送到服务器...

最佳答案

Spring security 现在提供与 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, "fakeuser@fakedom");
env.put(Context.SECURITY_CREDENTIALS, "1qaz!QAZ");

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

如果此方法有效,则说明配置有问题。

Good reading with examples .

关于java - Spring LDAP : creating connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011888/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com