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

无法使用Spring Security通过LDAP对目录(Active directory)进行身份验证,spring,spring-security,ldap,Spring,Spring Security,Ldap,我正在使用: -春季3.1.3 问题是我无法使用有效凭据通过LDAP连接Active Directory 我不知道这是否是由格式错误的模式或有关userdn或url的rootDn的配置问题引起的。虽然乍一看,似乎一切都是正确的 这是我当前的spring安全配置文件: ... <security:authentication-manager alias="authenticationManager"> <security:authentication-p

我正在使用: -春季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>

...
我在LDAP wiki上读到的52e代码解释并不完全正确,因为它同时启动了现有用户名和不存在用户名

我指的是:

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


不适合我。

我已经找到了我问题的答案

我在bindAuthentication中指定了用户搜索属性。之前我测试了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>

OU=我的公司,DC=域,DC=子域
(sAMAccountName={0})
真的
也许我可以帮助有类似问题的人

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>

它似乎也很好用

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