Аутентификация с использованием LDAP с Spring LDAP API и без использования Spring Security

Я использую плагин spring-ldap-core в моем загрузочном приложении Sprint. По сути, шаблон LDAPTemplate - http://docs.spring.io/spring-ldap/docs/current/apidocs/org/springframework/ldap/core/LdapTemplate.html

Я в основном хочу преобразовать приведенную ниже конфигурацию xml в java с помощью Spring LDAP API и не хочу использовать безопасность Spring.

Конфигурация xml, которую я хочу преобразовать, -

 <ldap-server id="ldapServer"
                 url="ldap://ad.company.com:389"
                 manager-dn="CN=serviceaccount,OU=Service Accounts,DC=ad,DC=company,DC=com"
                 manager-password="password"/>

    <authentication-manager>
        <ldap-authentication-provider
                server-ref="ldapServer"
                user-search-base="dc=ad,dc=company,dc=com"
                user-search-filter="sAMAccountName={0}"
                group-search-filter="member={0}"
                group-search-base="ou=Groups,dc=ad,dc=company,dc=com"
                group-role-attribute="cn"/>
    </authentication-manager>

Вот мой код Java ниже -

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.authentication.DefaultValuesAuthenticationSourceDecorator;

@Configuration
public class LdapConfiguration {

    @Bean
    public LdapContextSource contextSource(){
        LdapContextSource contextSource = new LdapContextSource();

        contextSource.setUrl("ldap://ad.company.com:389");
        contextSource.setBase("DC=ad,DC=company,DC=com");
        contextSource.setUserDn("CN=serviceaccount,OU=Service Accounts,DC=ad,DC=company,DC=com");
        contextSource.setPassword("password");
        contextSource.afterPropertiesSet();
        return contextSource;
    }


    @Bean
    public LdapTemplate ldapTemplate(){

        LdapTemplate template = new LdapTemplate(contextSource());
        try {
            template.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return template;
    }
}

Вот как я пытаюсь вызвать аутентификацию - метод, частью которого является этот фрагмент, возвращает логическое значение, если аутентификация происходит.

 AndFilter filter = new AndFilter();

    filter.and(new EqualsFilter("sAMAccountName", userloginName));

    return ldapTemplate.authenticate("OU=Service Accounts", filter.encode(), userPassword);

Это не работает, и я получаю следующую ошибку:

No results found for search, base: 'OU=Service Accounts'; filter: '(sAMAccountName=usernameIinput)'.

Я хочу знать, как с помощью LDAP API можно настроить следующие свойства xml?

group-search-filter="member={0}"
group-search-base="ou=Groups,dc=ad,dc=company,dc=com"
group-role-attribute="cn"/>

Кроме того, что еще мне не хватает? Почему это не работает? Любая помощь будет очень признательна!


person Tisha    schedule 21.10.2016    source источник


Ответы (1)


Я смог понять это.

// Соединение LDAP с использованием LDAPTemplate

@Configuration
public class LdapConfiguration {

    @Bean
    public LdapContextSource contextSource(){
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl("ldap://companyurl.com:389");
        contextSource.setUserDn("CN=serviceaccount,OU=Service Accounts,DC=ad,DC=company,DC=com");
        contextSource.setPassword("secretpassword");
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate(){
        LdapTemplate template = new LdapTemplate(contextSource());
        return template;
    }
}

// Часть аутентификации

AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("mailNickname", username));

Boolean authenticate = ldapTemplate.authenticate(base, filter.encode(), userpassword);
person Tisha    schedule 22.10.2016
comment
можем ли мы выполнить ldapTemplate.authenticate () без настройки contextSource.setUserDN () и contextSource.setPassword (). - person TX T; 27.03.2017
comment
@TXT, параметр userDN в contextSource является обязательным параметром. Без передачи этого параметра вы не сможете получить соединение ldap. - person Zubair Ahmed; 21.08.2019