繁体   English   中英

Spring Boot应用程序中的LDAP身份验证

[英]LDAP authentication in spring boot app

我对LDAP几乎一无所知,甚至对Spring安全性一无所知,但是我试图配置Spring Boot应用程序以针对ldap实例进行身份验证并被卡住。

我在adldap.company.com和dn = dc = ad,dc = company,dc = com的基础dn上获得了ldap服务器名称

我有一些执行简单绑定并工作的python代码。

LDAP_USERNAME = 'username@ad.company.com'
LDAP_PASSWORD = 'password'
base_dn = 'dc=ad,dc=company,dc=com' # not used for bind I guess, only search
try:
    ldap_client = ldap.initialize('ldap://adldap.company.com')
    ldap_client.set_option(ldap.OPT_REFERRALS,0)
    ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
except ldap.INVALID_CREDENTIALS as e:
    ldap_client.unbind()
    return 'Wrong username and password: %s' % e
except ldap.SERVER_DOWN:
   return 'AD server not available'

如果我运行此代码,它似乎已成功以“ username@ad.company.com”和密码“ password”绑定。

我也有一个WebSecurityConfig类,我认为应该处理auth:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/secure")
            .authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and()
            .httpBasic();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
            .userDnPatterns("uid={0}")
            .contextSource()
            .url("ldap://adldap.company.com");
            //.url("ldap://adldap.company.com/dc=ad,dc=company,dc=com");
    }
}

当我在应用程序中转到/ secure时,会弹出一个基本身份验证,但是随后尝试输入的任何内容都会使我获得401 Unauthorized。 我尝试了不带域的“ username@ad.company.com”,将这些内容放入了{0}@adldap.company.com之类的userDnPatterns中,以及其他一些东西。 我尝试过使用带有基本dn的不同URL。 似乎没有任何作用。 我想念什么?

另外,这是验证用户身份的正确方法吗? 我已经阅读了绑定身份验证以及有关绑定和搜索的内容,但是服务器不允许匿名绑定,因此我想我需要某种可以绑定并执行搜索的“应用程序用户”,对吗? 那个更好吗”?

Active Directory具有自己的用于用户身份验证的非标准语法,与通常的LDAP DN绑定不同。

Spring Security为Active Directory提供了专门的AuthenticationProvider。

尝试这个 :

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/secure")
            .authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and()
            .httpBasic();
    }

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

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
    }
    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("adldap.company.com", "ldap://adldap.company.com");
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        return provider;
    }
}

长话短说,问题在于Microsoft Active Directory LDAP不是“ Vanilla” LDAP,因此您需要以不同的方式连接到它。

工作解决方案在这里: https : //medium.com/@dmarko484/spring-boot-active-directory-authentication-5ea04969f220

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM