2015-05-19 34 views
2

我有我的AD服務器的SpringFramework安全4.0.0的工作設置。這在使用默認搜索過濾器的情況下工作良好,直到我發現AD數據庫中的用戶最初扭曲得比預期的要多得多。Spring ActiveDirectoryLdapAuthenticationProvider setSearchFilter方法

我的設置是爲遵循命名空間中的ActiveDirectoryLdapAuthenticationProvider豆:

<b:bean id="monFournisseurAD" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider"> 
    <b:constructor-arg value="subdom1.dom1.com" /> 
    <b:constructor-arg value="ldap://fsapps.company.uni:389/" /> 
    <b:constructor-arg value="dc=fsapps,dc=company,dc=uni" /> 
    <b:property name="searchFilter" value="(&amp;(userPrincipalName={0})(objectClass=user))" /> 
    <b:property name="userDetailsContextMapper"> 
     <b:bean class="org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper" /> 
    </b:property> 
    <b:property name="authoritiesMapper" ref="grantedAuthoritiesMapper" /> 
    <b:property name="convertSubErrorCodesToExceptions" value="true" /> 
</b:bean> 

然後我搬到了以下設置,而不是取消對域的依賴性:

<b:bean id="monFournisseurAD" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider"> 
    <b:constructor-arg value="" /> 
    <b:constructor-arg value="ldap://fsapps.company.uni:389/" /> 
    <b:constructor-arg value="dc=fsapps,dc=company,dc=uni" /> 
    <b:property name="searchFilter" value="(&amp;(sAMAccountName={0})(objectClass=user))" /> 
    <b:property name="userDetailsContextMapper"> 
     <b:bean class="org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper" /> 
    </b:property> 
    <b:property name="authoritiesMapper" ref="grantedAuthoritiesMapper" /> 
    <b:property name="convertSubErrorCodesToExceptions" value="true" /> 
</b:bean> 

現在的問題是searchFilter中的{0}替換,因此SpringFramework Security 4.0.0文檔正在替換username @ domain,其中domain似乎是AD服務器本身的默認域,在本例中爲fsapps.company.u因爲我的第一個構造函數參數是空的。 SpringFramework Security 4.0.0 Class ActiveDirectoryLdapAuthenticationProvider's documentation

問:有沒有辦法只替換username而不是username @ domain?任何其他建議如何我可以繞過這個問題?

注意:看完源代碼後。看起來預期的行爲應該如下所示:如果ActiveDirectoryLdapAuthenticationProvider類的構造函數的域參數是空字符串或全空白字符串,則它將設置爲null。當域設置爲空時,{0}模式的替換應該僅僅是沒有附加到域的用戶名,這應該完全按照我希望的方式進行:搜索sAMAccountName屬性等於用戶名和objectClass用戶的記錄。那麼,爲什麼它無法找到不在rootDN中的用戶作爲域(在我的例子中是第三個構造函數的參數fsapps.company.com)呢?我沒有問題,以匹配的用戶將具有一個userPrincipalName的形式:[email protected]而我無法匹配用戶與userPrincipalName的形式:[email protected]

注2:我發現了這個問題,還沒有解決。實際上,我的搜索過濾器非常好,Spring Security正確地使用它。問題是身份驗證是通過綁定到AD服務器並綁定到服務器來執行的,我必須使用[email protected](如果授權)或[email protected]。根據具有域的用戶名而不是沒有域的用戶名來檢查sAMAccountName屬性。如果添加了域,則沒有與作爲參數傳遞的值匹配的sAMAccountName。

回答

0

工作的解決方案是依賴使用userPrincipalName的第一個顯示的配置,並清空第一個構造函數的參數以將空字符串傳遞給構造函數。這樣,沒有什麼會被附加到用戶名,而不是輸入一個簡單的用戶名,用戶必須鍵入他們的完整用戶名與指定的域。因此,不要使用用戶名登錄,而是使用username @ domain。

另一種解決方案是調查是否可以綁定到AD數據庫,使用通用授權用戶代表其執行所有查詢。我還沒有調查過它是否可行(這是通過普通的LDAP完成的)以及它需要多少努力。如果有人試驗這個解決方案,最好在這裏發佈結果來分享這個解決方案。