2016-04-19 30 views
0

我創建一個本地LDAP服務器,並添加用戶「djiao」,密碼爲「123456 enter image description here使用什麼登錄名春LDAP身份驗證

試圖與春季安全和Spring引導來實現認證。我webconfig類如下:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .anyRequest() 
       .authenticated() 
       .and() 
      .formLogin(); 
    } 

    @Bean 
    public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() { 
     ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("", "ldap://localhost:10389"); 
     provider.setConvertSubErrorCodesToExceptions(true); 
     provider.setConvertSubErrorCodesToExceptions(true); 
     provider.setUseAuthenticationRequestCredentials(true); 
     return provider; 
    } 

    @Bean 
    public LoggerListener loggerListener() { 
     return new LoggerListener(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()); 
    } 

但是我似乎無法從登錄頁面登錄

  1. 如果我使用djiao(。 (LDAP:error code 34 - 給出的DN不正確:djiao1(0x64 0x6A 0x69 0x61 0x6F 0x31)無效];嵌套異常是javax.naming.InvalidNameException:[LDAP:錯誤代碼34 - 錯誤的DN給出:djiao1(0x64 0x6A 0x69 0x61 0x6F 0x31)無效]

  2. 如果我使用dn「cn = djiao,ou = Users, dc = example,dc = com「作爲用戶名,我將得到」Bad credentials「錯誤。並且密碼僅爲123456.

登錄的用戶名應該是什麼?或者我在websecurityconfig類中丟失了什麼?

+0

正如我所看到的,您正在使用正在尋找名爲SAMAccountName而不是uid的屬性的AD連接​​器。請檢查「正常」Ldap連接器。 –

+0

@ daniel.eichten我是Spring Security的新手。我需要使用普通的ldap連接器進行哪些更改? – Nasreddin

回答

2

由於您的代碼可以識別出您使用的是Spring-Boot。

這就是正在爲我們連接到LDAP

@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder authBuilder) throws Exception { 
     authBuilder 
      .ldapAuthentication() 
      .userSearchFilter("(sAMAccountName={0})") 
      .userSearchBase("dc=some,dc=domain,dc=com") 
      .groupSearchBase("ou=groups,dc=some,dc=domain,dc=com") 
      .groupSearchFilter("member={0}") 
      .contextSource() 
       .url("ldaps://<ldap-server>") 
       .port(639) 
       .managerDn("cn=binduser,ou=users,dc=some,dc=domain,dc=com") 
       .managerPassword("some pass") 
     ; 
    } 
} 
本質

那麼回事了userSearchFilter你必須定義不同的值。如果您使用除AD之外的任何LDAP,則您的過濾器應該由"(uid={0})"或者如果您不希望人們能夠使用該電子郵件,那麼您也可以使用"(mail={0})""(|(uid={0})(mail={0}))"這樣的組合,這樣就可以同時使用這兩者。

如果你使用ActiveDirectory - 我假設你不是基於你上面寫的 - 它應該是sAMAccountName如上所述,以便人們可以在域名中輸入他們的ID,如MYDOMAIN\myusername,這樣登錄就會是myusername

如果您需要連接到多個共享相同信息以用於HA目的的LDAP服務器,則可以通過調用.contextSource().url()來完成此操作。如果它們攜帶不同的,例如BTW

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder authBuilder) throws Exception { 
    authBuilder 
     .ldapAuthentication() 
     .userSearchFilter("(sAMAccountName={0})") 
     .userSearchBase("dc=emea,dc=domain,dc=com") 
     .groupSearchBase("ou=groups,dc=emea,dc=domain,dc=com") 
     .groupSearchFilter("member={0}") 
     .contextSource() 
      .url("ldaps://<emea-ldap-server>") 
      .port(639) 
      .managerDn("cn=binduser,ou=users,dc=emea,dc=domain,dc=com") 
      .managerPassword("some pass") 
     .and() 
     .and() 
     .ldapAuthentication() 
     .userSearchFilter("(sAMAccountName={0})") 
     .userSearchBase("dc=ap,dc=domain,dc=com") 
     .groupSearchBase("ou=groups,dc=ap,dc=domain,dc=com") 
     .groupSearchFilter("member={0}") 
     .contextSource() 
      .url("ldaps://<ap-ldap-server>") 
      .port(639) 
      .managerDn("cn=binduser,ou=users,dc=ap,dc=domain,dc=com") 
      .managerPassword("some pass") 

    ; 
} 

:「EMEA」,「美國」,「AP」,您可以使用結合這些要求,這也可以讓你不同的認證機制與LDAP結合像InMemory(缺省管理員借殼)和/或JDBC。

+0

它現在有效。你可以看看我的其他[後](http://stackoverflow.com/questions/36748592/uncategorized-exception-for-using-using-correct-credentials-in-ldap-authentication)關於認證對機構ldap服務器? – Nasreddin