Spring 使用多后缀值/域验证ldap用户

Spring 使用多后缀值/域验证ldap用户,spring,spring-security,active-directory,ldap,spring-ldap,Spring,Spring Security,Active Directory,Ldap,Spring Ldap,我正在尝试使用SpringLDAP安全性和SpringLDAP进行身份验证,然后查询广告树 以下是我的配置文件- <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="h

我正在尝试使用SpringLDAP安全性和SpringLDAP进行身份验证,然后查询广告树

以下是我的配置文件-

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:ldap="http://www.springframework.org/schema/ldap"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.2.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/ldap 
        http://www.springframework.org/schema/ldap/spring-ldap.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd">

    <http use-expressions="true">
        <form-login login-page="/myApp/ldap" default-target-url="/myApp/ldap/config"
            authentication-failure-url="/myApp/ldap?error=true" />
        <logout />
    </http>

    <beans:bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="location">
            <beans:value>classpath:/ldap.properties</beans:value>
        </beans:property>
        <beans:property name="SystemPropertiesMode">
            <beans:value>2</beans:value>
        </beans:property>
    </beans:bean>

    <beans:bean id="adAuthenticationProvider" scope="prototype"
        class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
        <!-- the domain name (may be null or empty). If no domain name is configured, it is assumed that the username will always contain the domain name. -->
        <beans:constructor-arg index="0" value="${sample.ldap.domain}" />
        <!-- an LDAP url (or multiple URLs) -->
        <beans:constructor-arg index="1" value="${sample.ldap.url}" />
        <!-- Determines whether the supplied password will be used as the credentials in the successful authentication token. -->
        <beans:property name="useAuthenticationRequestCredentials"
            value="true" />
        <!-- by setting this property to true, when the authentication fails the error codes will also be used to control the exception raised. -->
        <beans:property name="convertSubErrorCodesToExceptions"
            value="true" />
    </beans:bean>

    <authentication-manager erase-credentials="false">
        <authentication-provider ref="adAuthenticationProvider" />
    </authentication-manager>

    <beans:bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="location">
            <beans:value>classpath:/ldap.properties</beans:value>
        </beans:property>
        <beans:property name="SystemPropertiesMode">
            <beans:value>2</beans:value> <!-- OVERRIDE is 2 -->
        </beans:property>
    </beans:bean>

    <ldap:context-source id="contextSource" 
                         url="${sample.ldap.url}"
                         base="${sample.ldap.base}" 
                         referral="follow"
                         authentication-source-ref="authenticationSource" 
                         base-env-props-ref="baseEnvironmentProperties"/>

    <util:map id="baseEnvironmentProperties">
        <beans:entry key="com.sun.jndi.ldap.connect.timeout" value="60000" />
        <beans:entry key="java.naming.ldap.attributes.binary" value="objectGUID objectSid"/>
    </util:map>

    <beans:bean id="authenticationSource"
        class="org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource" />

    <ldap:ldap-template id="ldapTemplate"
        context-source-ref="contextSource" />

</beans:beans>
这些设置对于以下登录很有效-

用户名-example@example.com或示例 密码-废话

但当我尝试时失败了- 用户名-example2@example.net或示例 密码-blah2

这两个都是有效的登录,并已通过使用AD Explorer登录进行验证

似乎我需要更新我的配置以支持UPN后缀/域,因为默认情况下可以正常工作,而其他情况则不行


是否有一种方法可以附加到此配置文件以支持此逻辑,支持对多个域进行身份验证/查询

它不允许我使用配置的UPN后缀登录的原因是
ActiveDirectoryLdapAuthenticationProvider
似乎在假设UPN后缀始终与域名相同

请参阅本帖-


我认为应该有更好的方法来处理这个问题,或者可能有更好的身份验证库。

要解释@NewBee的解决方案:

1
ActiveDirectoryLdapAuthenticationProvider

  • 使用Active Directory配置约定的专用
    LDAP身份验证提供程序
  • 它将使用Active Directory或(或自定义的
    searchFilter
    )以
    username@domain
    。如果
    用户名
    尚未以
    域名
    结尾,则将通过将配置的域名附加到
    身份验证请求中提供的用户名
    来构建
    userPrincipalName
    。如果未配置域名,则假定用户名将始终包含域名
  • 用户权限是从
    memberOf
    属性中包含的数据获得的
2
Spring Security中的LDAP身份验证

  • 从登录名中获取唯一的LDAP
    可分辨名称
    ,或DN

    • 这通常意味着在目录中执行
      搜索
      ,除非事先知道用户名到DNs的确切映射。因此,用户可能在登录时输入自己的姓名,但用于对LDAP进行
      身份验证的实际姓名将是完整的DN,例如
      uid=(用户名),ou=users,dc=springsource,dc=com
  • 验证用户
    ,方法是将
    绑定为该用户,或通过对DN目录项中的密码属性执行远程比较操作

  • 正在加载用户的权限列表

以供参考。另外,在
ActiveDirectoryLdapAuthenticationProvider
中,您可以编写一个函数来读取多个后缀,就像库一样

sample.ldap.url=ldap://xxx.xxx.xxx.xxx:3268
sample.ldap.base=dc=example,dc=com
sample.ldap.clean=true
sample.ldap.directory.type=AD
sample.ldap.domain=example.com