首页 文章

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

提问于
浏览
2

我对LDAP几乎一无所知,更不了解 spring 安全性,但我正在尝试配置一个Spring启动应用程序来对ldap实例进行身份验证并且卡住了 .

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

我有一些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”密码“密码” .

我还有一个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");
    }
}

当我在应用程序中进入/安全时,我会弹出一个基本的身份验证,但是我尝试输入的任何内容都会让我获得401未授权 . 我尝试了“username@ad.company.com”,没有域名,将这些东西放在userDnPatterns中,如{0} @adldap.company.com和其他一些东西 . 我尝试过使用不同的URL和基本的dn . 似乎没什么用 . 我错过了什么?

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

2 回答

  • 3

    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;
        }
    }
    
  • 0

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

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

相关问题