代码之家  ›  专栏  ›  技术社区  ›  Mike Q

使用LDAP对使用Spring安全性的Adam进行身份验证

  •  6
  • Mike Q  · 技术社区  · 14 年前

    我正在尝试使用Spring Security获取Java应用程序,与我安装的本地亚当实例进行对话。

    我已经成功地安装了Adam并安装了如下…

    • 本地主机上运行的实例:389
    • 根是 O=Company
      • 一个叫孩子的孩子 OU=Company Users (组织AlUnit)
        • 一个外婆叫 CN=Mike Q (用户)
        • uid = mike password = welcome

    然后我设置了SpringSecurity(版本3.0.3、SpringFramework3.0.4和SpringLDAP 1.3.0)。弹簧文件

      <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查询中以明文形式发送密码的一般用例/最佳实践是什么?
    2 回复  |  直到 8 年前
        1
  •  4
  •   Mike Q    14 年前

      <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"/>
    

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

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

    cn=People (uid={0})

        @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)
    

        2
  •  0
  •   Hesham Masoud    11 年前