使用Spring对安全性使用LDAP进行身份validation

我正在尝试使用spring-security来获取Java应用程序,以便与我设置的本地ADAM实例进行通信。

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

  • 在localhost上运行的实例:389
  • 根是O=Company
    • 一个叫OU=Company Users (orgnizationalUnit)的孩子
      • 一个名为CN=Mike Q (用户)的granchild
      • uid = mikepassword = welcome

然后我设置了spring-security(版本3.0.3,spring-framework 3.0.4和spring-ldap 1.3.0)。 spring文件

       

和TestAuthentication

 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.(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.(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) 

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

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

  • LDAP区分大小写吗?
  • 最好避免使用空间吗?
  • 避免在LDAP查询中以明文forms发送密码的一般用例/最佳实践是什么?

好的,所以我花了很多时间来解决这个问题。

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

经过一些试验和错误后,这是一个有效的配置,用户可以正常搜索。 (你可以使用安全命名空间重写它,但在我正在研究它时,使用原始bean定义更清楚)。

             cn={0},cn=People           

关键是

  

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

使用BindAuthentication时

 cn={0},cn=People 

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

配置UserSearch时

    

我无法让它与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) 

希望这一切都有助于其他人。

我通过添加您尝试在同一基本DN中作为管理员角色成员使用的用户来解决此问题。 希望有所帮助

Interesting Posts