gpt4 book ai didi

java - 使用 LDAP 对使用 spring security 的 ADAM 进行身份验证

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:23 24 4
gpt4 key购买 nike

我正在尝试使用 spring-security 获取一个 Java 应用程序来与我设置的本地 ADAM 实例对话。

我已经成功安装了 ADAM 并设置如下....

  • 在本地主机上运行的实例:389
  • 根是 O=Company
    • 一个 child 叫做 OU=Company Users (orgnizationalUnit)
      • 一个叫 CN=Mike Q 的孙子(用户)
      • uid = mikepassword = welcome

然后我设置了 spring-security(版本 3.0.3、spring-framework 3.0.4 和 spring-ldap 1.3.0)。 Spring 锉

  <security:ldap-server id="contextSource" url="ldap://localhost:389/o=Company"/>

<security:authentication-manager>
<security:ldap-authentication-provider user-dn-pattern="uid={0},ou=Company Users"/>
</security:authentication-manager>

<bean class="com.xxx.test.TestAuthentication" lazy-init="false"/>

和测试身份验证

public class TestAuthentication
{
@Autowired
private AuthenticationManager authenticationManager;

public void initialise()
{
Authentication authentication = new UsernamePasswordAuthenticationToken( "mike", "welcome" );
Authentication reponseAuthentication = authenticationManager.authenticate( authentication );
}
}

运行这个我得到以下错误

Caused by: javax.naming.AuthenticationException: [LDAP: error code 49 - 8009030C: LdapErr: DSID-0C090336, comment: AcceptSecurityContext error, data 2030, vece]
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3041)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2987)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2789)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2703)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
at org.springframework.ldap.core.support.LdapContextSource.getDirContextInstance(LdapContextSource.java:43)
at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:254)

如果有人能指出我哪里出错了,我将不胜感激。此时我只想使用 LDAP 验证输入的用户/密码,没有比这更复杂的了。

我也对一些一般点感兴趣,因为这是我第一次涉足 LDAP 世界。

  • LDAP 区分大小写吗?
  • 是否最好避免使用空格?
  • 避免在 LDAP 查询中以明文形式发送密码的一般用例/最佳做法是什么?

最佳答案

好吧,我花了很多时间来解决这个问题,这就是答案。

错误代码 2030 表示用户的 DN 无效。

经过一些试验和错误后,这里的配置可以正常工作并且可以正确地进行用户搜索。 (您可能可以使用安全命名空间重写它,但在我处理它时,使用原始 bean 定义会更清楚)。

  <bean id="contextSource"
class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://localhost:389/cn=Sandbox,dc=ITOrg"/>
<property name="userDn" value="cn=superuser,cn=People,cn=Sandbox,dc=ITOrg"/>
<property name="password" value="xxxxxx"/>
</bean>

<bean id="ldapAuthProvider"
class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<constructor-arg>
<bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
<constructor-arg ref="contextSource"/>
<property name="userDnPatterns">
<list>
<value>cn={0},cn=People</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>

<bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<constructor-arg index="0" value="cn=People"/>
<constructor-arg index="1" value="(cn={0})"/>
<constructor-arg index="2" ref="contextSource"/>
</bean>

关键是

<property name="userDn" value="cn=superuser,cn=People,cn=Sandbox,dc=ITOrg"/>

在上下文源中指定 userDn 时,它必须是完整的 DN(它不只是附加它做 url 中提供的基础(构造函数 arg)。

使用 BindAuthentication 时

<value>cn={0},cn=People</value>

此值是上下文源 baseDn 之上的后缀。

配置用户搜索时

    <constructor-arg index="0" value="cn=People"/>
<constructor-arg index="1" value="(cn={0})"/>

我无法让它与 cn=People 在第二个 arg 中一起工作,但这似乎工作正常。请注意,您可以使用用户的属性,例如(uid={0})

下面是一些使用 bean 定义的示例代码...

    @Autowired
private LdapUserSearch ldapUserSearch;

@Autowired
private AuthenticationProvider authenticationProvider;

public void initialise()
{
DirContextOperations dirContextOperations = ldapUserSearch.searchForUser( "username" );

Authentication authentication = authenticationProvider.authenticate( new UsernamePasswordAuthenticationToken( "username", "password" ) );
}

一些其他随机花絮...

Error 52b - Invalid password


[LDAP: error code 32 - 0000208D: NameErr: DSID-031521D2, problem 2001 (NO_OBJECT), data 0, best match of: 'CN=Sandbox,DC=ITOrg'
- This means the user is not in the administrator role (probably)

希望这一切对其他人有帮助。

关于java - 使用 LDAP 对使用 spring security 的 ADAM 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3788841/

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