簡體   English   中英

簡單 Spring 引導 LDAP 身份驗證示例不適用於 ActiveDirectory

[英]Simple Spring Boot LDAP authentication example does not work with ActiveDirectory

我找到了一個非常簡單的 LDAP 身份驗證示例,它使用嵌入式 LDAP 服務器工作得很好: https://github.com/asbnotebook/spring-boot/tree/master/spring-security-embedded-ldap-example 這正是我所需要的——添加了一個配置 class,現在所有用戶都需要在訪問該應用程序之前登錄。

由於我們的 AD(本地服務器,不是 Azure AD)需要 userDN 和密碼才能訪問,我將其添加到示例代碼中,還修改了 url、base dn 等。

當我嘗試登錄時,總是收到“憑據錯誤”錯誤消息。 然后我逐步查看代碼,發現 Spring LDAP 代碼成功地從 AD 中檢索了一些用戶數據(我在“userDetails”object 中找到了用戶 email 地址,只有在 AD 中才知道),但是設置了“密碼”字段到 null。然后將此 null 值與用戶輸入的密碼進行比較,失敗並在 function org.springframework.security.authentication.dao.additionalAuthenticationChecks() 中拋出 BadCredentialsException。

所以現在我有兩個問題:

  1. 為什么“密碼”屬性設置為 null? 我的理解是它應該包含密碼 hash。我用 ldapsearch 檢查了 AD 響應,但我沒有看到任何看起來像密碼 hash 的東西。但是 userDN 確實可以與其他應用程序一起使用,所以它可能不是 userDN AD 的問題帳戶。 請告知如何正確找回密碼信息。

  2. 我相信該示例不處理密碼哈希。 用於預加載示例應用程序的嵌入式 LDAP 服務器的 LDIF 文件只包含 userPassword 屬性的明文密碼。 此外,示例代碼中的 passwordEncoder 看起來像一個無操作編碼器。 我應該如何更改它以使其與 AD 一起使用?

這是我的代碼:

package com.asbnotebook.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.ldap.userdetails.LdapUserDetailsManager;

@Configuration
public class LdapSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() {
        
        var cs = new DefaultSpringSecurityContextSource("ldaps://ad.company.local/dc=company,dc=local");
        cs.setUserDn("cn=robot1,ou=robots");
        cs.setPassword("secret");
        cs.afterPropertiesSet();

        var manager = new LdapUserDetailsManager(cs);
        manager.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=company_user", "cn"));
        manager.setGroupSearchBase("ou=company_groups");

        return manager;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
}

在考慮了Gabriel Luci 的評論之后,我現在找到了一種使用 ActiveDirectory 進行身份驗證的簡單方法:

package com.asbnotebook.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;

@Configuration
public class LdapSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        ActiveDirectoryLdapAuthenticationProvider adProvider =
                new ActiveDirectoryLdapAuthenticationProvider(
                        "company.de","ldaps://ad.company.local","dc=company,dc=local");
        adProvider.setConvertSubErrorCodesToExceptions(true);
        adProvider.setUseAuthenticationRequestCredentials(true);
        auth.authenticationProvider(adProvider);
        auth.eraseCredentials(false);
    }
}

可以使用 email 地址或 sAMAccountName 登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM