使用Spring security 2.0.3进行LDAP身份验证

时间:2012-10-19 13:39:08

标签: active-directory spring-security ldap spring-ldap

我正在尝试使用spring security 2.0.3进行LDAP身份验证

这是我的配置

<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <constructor-arg value="ldap://IP:3268"/>
        <property name="base" value="dc=alliance",dc=com,OU=Users,OU=India"/>
        <property name="userDn" value="sAMAccountName=username" />
        <property name="password" value="password" />
    </bean>



    <bean id="ldapProvider" class="org.springframework.security.providers.ldap.LdapAuthenticationProvider">
        <sec:custom-authentication-provider/>
        <constructor-arg>
            <bean class="org.springframework.security.providers.ldap.authenticator.BindAuthenticator">
                <constructor-arg ref="contextSource"/>
                <!--<property name="userSearch" ref="ldapSearchBean"/>-->
                <property name="userDnPatterns">
                   <list><value>sAMAccountName={0}</value></list>
                 </property>
            </bean>
        </constructor-arg>
        <constructor-arg>
            <bean class="org.springframework.security.ldap.populator.DefaultLdapAuthoritiesPopulator">
                <constructor-arg ref="contextSource"/>
                <constructor-arg value="ou=groups"/>
                <property name="groupSearchFilter" value="member={0}"/>
                <property name="groupRoleAttribute" value="ou"/>
                <property name="rolePrefix" value="ROLE_"/>
                <property name="searchSubtree" value="true"/>
                <property name="convertToUpperCase" value="true"/>
            </bean>
        </constructor-arg>
    </bean>

Maven条目集如下

<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>2.0.3</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap</artifactId>
            <version>1.2.1</version>
        </dependency>

我得到的例外是

  

[BindAuthenticator,2329165 @ qtp-24103634-0] - 无法绑定为sAMAccountName = csepena:org.springframework.ldap.AuthenticationException:[LDAP:错误代码49 - 80090308:LdapErr:DSID-0C090334,评论:AcceptSecurityContext错误,数据525,vece

我应该在哪里提及域名?

1 个答案:

答案 0 :(得分:1)

如果您在线搜索错误消息,您会发现this page of Active Directory errors之类的内容,它会在顶部列出您的错误:

Common Active Directory LDAP bind errors:

80090308: LdapErr: DSID-0C09030B, comment: AcceptSecurityContext error, data 525, v893
HEX: 0×525 – user not found
DEC: 1317 – ERROR_NO_SUCH_USER (The specified account does not exist.)
NOTE: Returns when username is invalid.

因此,根据AD,用户不存在。您已尝试将sAMAccountName属性用作DN模式的一部分,这不会起作用,因为它是LDAP属性。您需要使用搜索来首先按此属性的值定位用户。有关如何在本网站和网络上的其他地方执行此操作的示例。看起来你已经尝试了,因为你注释掉了一个搜索bean。如果这不起作用,你应该解释你的问题出了什么问题。

事实上,它很可能失败了,因为您的上下文源存在一些问题。 userDn property的值是错误的。它必须是目录中的有效可分辨名称,“sAMAccountName = username”不是。 “base”属性看起来也不正确。它通常应该是根(dc=mycompany,dc=com在末尾)的树。所以应该是ou=mygroup,ou=mycountry,dc=mycompany,dc=com

最后,您不应该使用2.0.3版。它已知安全漏洞。随时了解您正在使用的补丁和新版本的库 - number 6 in the OWASP "top ten"。如果您遇到已修复的错误,检查最新版本也是有意义的。