无法通过LDAP使用Spring Security对目录(Active Directory)进行身份验证

时间:2014-10-07 10:34:29

标签: spring spring-security ldap

我正在使用: - 春季3.1.3

问题是我无法使用有效凭据通过LDAP与Active Directory建立连接。

我不知道是由于格式错误的模式还是有关userdn或url的rootDn的配置问题。虽然乍一看似乎一切都是正确的。

这是我当前的spring安全配置文件:

...

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="ldapAuthProvider" />
    </security:authentication-manager>

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

    <bean id="contextSource" 
            class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
         <constructor-arg value="ldap://remotehost:port/OU=My%20Company,dc=domain,dc=subdomain"/>
         <property name="userDn" value="CN=managerUserCN,OU=Users,OU=Test Accounts,OU=My Company,dc=domain,dc=subdomain/>
         <property name="password" value="thePass"/>
    </bean>

...

*我已通过描述性数据替换了真实的网址,组织,群组等

*这是一个通过 sAMAccountName 搜索的请求。

doAuthentication抛出的NamingException:bindWithDn是下一个:

*org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1*

我在LDAP维基上读到的52e代码解释并不完全正确,因为它既启动了现有的用户名,又启动了不存在的用户名。

我参考:

注意:当用户名有效但密码/凭证无效时返回。将防止显示大多数其他错误。

不适合我。

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案。

我在bindAuthentication中指定了user-Search属性。以前我测试过userSearch选项而不包括基目录(第一个参数)。所以,几乎对我而言,它是强制性的,让身份验证有效。

在代码中:

<bean id="ldapAuthProvider"
  class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
  <constructor-arg>
    <bean id="bindAuthenticator" class="org.springframework.security.ldap.authentication.BindAuthenticator">
        <constructor-arg ref="contextSource" />
        <property name="userSearch" ref="userSearch"/>
    </bean>
  </constructor-arg>
</bean>

<bean id="userSearch"
    class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
    <constructor-arg>
        <value>OU=My Company,DC=domain,DC=subdomain</value>
    </constructor-arg>
    <constructor-arg>
        <value>(sAMAccountName={0})</value>
    </constructor-arg>
    <constructor-arg ref="contextSource" />
    <property name="searchSubtree">
        <value>true</value>
    </property>
</bean>

也许我可以帮助有类似问题的人。

pD:另一种选择是使用指定的ActiveDirectoryLdapAuthenticationProvider

 <bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
    <constructor-arg value="domain.subdomain" />
    <constructor-arg value="ldap://host:port" />
    <property name="convertSubErrorCodesToExceptions" value="true"/>
</bean>

它似乎也很好。