2013-04-29 48 views
1

我使用LDAP進行身份驗證,並且對於特定域中的用戶,一切正常。但是我很難理解我如何驗證第二個域下的用戶。我下面顯示的當前配置指定了用戶搜索庫中的第一個域。我刪除了這個參數,希望它能搜索所有的域名,但那不起作用。出現提示時,我也嘗試將域指定爲用戶名的一部分,例如domain \ user,但這也不起作用。我可以配置Spring Security以提示輸入域名以及用戶名和密碼

<security:authentication-manager alias="authenticationManager"> 
     <security:ldap-authentication-provider 
      user-search-filter="(samaccountname={0})" 
      user-search-base="dc=domain,dc=company,dc=com" 
      user-context-mapper-ref="userContextMapper" > 
     </security:ldap-authentication-provider> 
</security:authentication-manager> 

<security:ldap-server 
    url="ldap://some.url.com:3000" 
    manager-dn="CN=USER1,OU=FacMgr,OU=FAC,OU=Exchange,dc=domain,dc=company,dc=com" 
    manager-password="xxxx"/> 

我需要創建一個自定義搜索,如果有的話,有人可以在這種情況下提供一個例子嗎?

回答

0

看起來好像您使用的是Active Directory,在這種情況下,我想知道爲什麼您不使用更基本的ActiveDirectoryLdapAuthenticationProvider類。

無論如何,您應該能夠通過擴展LdapAuthenticationProvider或ActiveDirectoryLdapAuthenticationProvider並將相應的域傳遞給超類的方法來完成所需的工作。

創建接受兩個不同LdapAuthenticator對象的構造函數,並添加在doAuthentication方法的catch (UsernameNotFoundException notFound)聲明第二個「嘗試」語句(對不正確的憑據檢查後)。如果第一個驗證器失敗,請使用任何方法讓getAuthenticator方法嘗試第二個驗證器。

這種方法應該可行,但如果兩個域有jsmith一個用戶名,但所涉及的用戶駐留在第二,你可能會遇到的問題 - 那就是,這是不是一個特別好的解決,但它的解決方案。

正確構建這一切時,使用自定義驗證過濾器(延長UsernamePasswordAuthenticationFilter),並有LdapAuthenticationProvider.getAuthenticator()方法從由過濾器(在您的自定義登錄表單)的值來標識域。

您可能需要不同的經理帳戶,但希望您繼續下去。

+0

如果您還沒有解決您的問題呢,我正好橫跨[這顆寶石(http://www.coderanch.com/t/485524/Spring/SpEL),而尋找用於將動態字段注入SpEL表達式的方法。從外觀上看,你應該能夠在'ldap.xml'配置文件中指定兩個不同的LDAP bean,然後根據域輸入引用相關的bean(即通過一個攔截登錄請求的控制器並轉發它動態)通過註釋。具體而言,請查找哪些引用列出了「TaxRuleProcessor」對象的註釋。這很漂亮。 – cabbagery 2013-05-02 15:56:43

0

使這項工作的訣竅是使用ActiveDirectoryLdapAuthenticationProvider。要做到這一點,只需做如下修改:

在resources.groovy:

// Domain 1 
ldapAuthProvider1(ActiveDirectoryLdapAuthenticationProvider, 
     "mydomain.com", 
     "ldap://mydomain.com/" 
) 

// Domain 2 
ldapAuthProvider2(ActiveDirectoryLdapAuthenticationProvider, 
     "mydomain2.com", 
     "ldap://mydomain2.com/" 
) 

Config.groovy中:

grails.plugin.springsecurity.providerNames = ['ldapAuthProvider1', 'ldapAuthProvider2'] 

這是所有你需要的代碼。您幾乎可以刪除Config.groovy中的所有其他grails.plugin.springsecurity.ldap。*設置,因爲它們不適用於此AD設置。

文檔: http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ldap-active-directory

相關問題