1

你好,我有使用LDAP創建簡單的登錄問題。我已經從spring.io網站下載了入門項目:link它與ldif文件完美結合,但我想用運行ldap服務器替換它。我已經嘗試了好幾天沒有進展。我與這段代碼(在WebSecurityConfig取代入門項目)春季啓動LDAP安全

@Configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

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

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

    @Bean 
    public AuthenticationManager authenticationManager() { 
     return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider())); 
    } 
    @Bean 
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() { 
     ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://ip:port/", "ou=GROUP,dc=domain,dc=com"); 
     provider.setConvertSubErrorCodesToExceptions(true); 
     provider.setUseAuthenticationRequestCredentials(true); 

     return provider; 
    } 
} 

如果我嘗試用格式「用戶名」,「密碼」控制檯輸出良好的用戶名和密碼登錄最好的結果:ActiveDirectoryLdapAuthenticationProvider:活動目錄身份驗證失敗:提供的密碼無效

如果我使用「[email protected]」和良好的密碼,頁面只是沒有輸出到控制檯重新加載。

如果我使用隨機用戶名和密碼控制檯:Active Directory身份驗證失敗:提供的密碼無效

有人能幫忙嗎?

+0

正如你在ActiveDirectoryLdapAuthenticationProvider的構造函數中使用「空」的域名,則必須使用「的UserPrincipalName」([email protected])來驗證 您可以啓用日誌記錄,以找出爲什麼這不工作或如果你不使用LDAPS或StartTLS擴展操作的網絡跟蹤會肯定地告訴如果LDAP BIND請求成功 –

回答

0

正如評論所說我已經打開了記錄,發現這個問題是同爲「[email protected]」了。

問題已在aciveDirectoryLdapAuthenticationProvider()有在它3點的問題。

  1. 我已刪除OU組從根DN並添加域,所以我們可以只使用用戶名進行登錄。

    ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://ip:port/", "dc=domain,dc=com"); 
    
  2. 改變searchfilter提供商的

    provider.setSearchFilter("(&(objectClass=user)(sAMAccountName={0}))"); 
    
  3. 最後我必須更改ActiveDirectoryLdapAuthProvider searchForUser方法,因爲它與「[email protected]」匹配,而sAMAccountName不是「用戶名」。這:

    return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context, 
           searchControls, searchRoot, searchFilter, 
           new Object[] { bindPrincipal });  
    

    替換爲:

    return SpringSecurityLdapTemplate.searchForSingleEntryInternal(context, 
          searchControls, searchRoot, searchFilter, 
          new Object[] { username }); 
    

完全aciveDirectoryLdapAuthenticationProvider():

@Bean 
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() { 
     ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://ip:port/", "dc=domain,dc=com"); 
     provider.setSearchFilter("(&(objectClass=user)(sAMAccountName={0}))"); 
     provider.setConvertSubErrorCodesToExceptions(true); 
     provider.setUseAuthenticationRequestCredentials(true); 
     return provider; 
    } 

有人可以提供第二/第三個問題更好的解決方案?也許更好的searchfilter?我在ldap中沒有任何與使用ActiveDirectoryLdapAuthProvider的bindPrincipal的「[email protected]」格式匹配的字段。

+0

嗨@ user3551808,可以請你告訴我怎麼和你在哪裏打開日誌記錄?我也有類似的錯誤,我希望看到更多的日誌記錄,但它似乎並沒有發生,我用logback.xml,我只是做了在logback.xml的開始此項:'調試=‘真’,但它似乎沒有幫助。 –