带有MD5的Spring Security 3.1身份验证LDAP

Spring security 3.1 authentication LDAP with md5

当我们尝试使用spring身份验证管理器进行身份验证时,它会显示"错误的凭据":

1
2
Authentication request = new UsernamePasswordAuthenticationToken("john","johnldap");
result = authenticationManager.authenticate(request);

这是SecurityApplicationContext.xml文件:

1
2
3
4
5
        <ldap-authentication-provider server-ref="ldapLocal"
            user-dn-pattern="uid={0},ou=People,dc=example,dc=com">        
        </ldap-authentication-provider>
    </authentication-manager>
    <ldap-server url="ldap://127.0.0.1:389/dc=example,dc=com" manager-dn="admin" manager-password="xxxxxxxx" id="ldapLocal"  />

但是使用" ldapsearch ",我们可以成功连接:

1
ldapsearch -D"uid=john,ou=People,dc=example,dc=com" -w johnldap  -L"objectClass=*"

最初,我们认为问题是我们必须告诉spring在调用LDAP之前对密码进行md5加密。因此,我们将其添加到applicationSecurtyContext.xml:

1
2
3
4
5
6
7
8
9
10
11
    <beans:bean id="passwordEncoder"  class="org.springframework.security.authentication.encoding.Md5PasswordEncoder">
    </beans:bean>
   
        <ldap-authentication-provider server-ref="ldapLocal"
            user-dn-pattern="uid={0},ou=People,dc=example,dc=com">  
         <password-compare>
            <password-encoder ref="passwordEncoder"> </password-encoder>
        </password-compare>
        </ldap-authentication-provider>
    </authentication-manager>
    <ldap-server url="ldap://127.0.0.1:389/dc=example,dc=com" manager-dn="admin" manager-password="xxxxxxxx" id="ldapLocal"  />

但是当我们添加标签时,它会显示:

1
LDAP: error code 34 - invalid DN]

这是怎么了?


如果我没有记错的话,user-dn-pattern不应包含根dn,因为它将被自动附加。因此,请尝试使用:

1
user-dn-pattern="uid={0},ou=People">

如果只想进行简单的绑定身份验证,我认为您不需要password-encoder


我花了很多时间尝试连接spring安全,看着stackoverflow我也认为编码可能有问题,因为密码在md5中,尽管我必须单独添加上述根dn,密码才能被编码通过ldap服务器。以下是我的工作版本:

1
2
3
4
5
6
7
<ldap-server url="ldap://dsa.company.com:389/" manager-dn="cn=manager,dc=company,dc=com"
    manager-password="pass"></ldap-server>

    <ldap-authentication-provider
        user-dn-pattern="cn={0},ou=people,dc=company,dc=com"
        group-search-base="ou=groups,dc=company,dc=com" />
</authentication-manager>