аутентификация шаблона Spring ldap - PullRequest
0 голосов
/ 07 сентября 2018

Я пытаюсь аутентифицировать пользователя через Spring ldap. Ниже приведен код для инициализации шаблона ldap.

contextSource = new LdapContextSource();
contextSource.setUrl("ldaps://ldap.example.com");
contextSource.setBase("DC=example,DC=com");
contextSource.setUserDn("backend-app");
contextSource.setPassword("password");
contextSource.afterPropertiesSet();

PoolingContextSource pooledContextSource = new PoolingContextSource(); 
pooledContextSource.setDirContextValidator(new 
DefaultDirContextValidator());
pooledContextSource.setContextSource(contextSource);

ldapTemplate =  new LdapTemplate(pooledContextSource); 
ldapTemplate.afterPropertiesSet();

Когда я пытаюсь использовать метод аутентификации ldapTemplate, он возвращает false.

// below line fails
ldapTemplate.authenticate("OU=Service Accounts,OU=Pseudo-Users", "frontend-web", "password");

Но когда я использую контекст каталога, он работает

DirContext ctx = null;
try {
  ctx = contextSource.getContext("frontend-web", "password");
  return true;
} catch (Exception e) {
  logger.error("Login failed", e);
  return false;
} finally {
  LdapUtils.closeContext(ctx);  
}

Вопрос 1: Есть ли способ заставить метод ldaptemplate authenticate работать?

Вопрос 2: Почему бы нам не предоставить baseDn, когда мы непосредственно используем DirectoryContext. Как ldap знает, где найти пользователя "frontend-web". Ищет ли он весь каталог для пользователя "frontend-web"

Может ли кто-нибудь помочь.

1 Ответ

0 голосов
/ 02 ноября 2018

Вопрос 1 - Я столкнулся с точно такой же проблемой .... все еще не понял, как использовать мой существующий шаблон LDAP. Я ввел конфигурацию ldap в свой сервис напрямую и создал новый «приватный» ldapTemplate, который не объединяется в пул, например:

public class AuthController {

    private final LdapTemplate ldapTemplate;

    AuthController(MyLdapConfig config) {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(config.getUrl());
        contextSource.setUserDn(config.getUserDn());
        contextSource.setPassword(config.getPassword());
        contextSource.afterPropertiesSet();

        ldapTemplate = new LdapTemplate(contextSource);
        ldapTemplate.setIgnorePartialResultException(true);
    }

    @PostMapping
    public ResponseEntity authenticate(@RequestBody AuthenticationRequest authenticationRequest) {
        if (authenticateUser(authenticationRequest.getUsername(), authenticationRequest.getPassword())) {
            return ResponseEntity.ok().build();
        }
        log.warn("authentication failed for {}", authenticationRequest.getUsername());
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }

    // authenticateUser method omitted
}

Вопрос 2 - да

поиск всегда начинается с корня дерева (пустой путь)

https://docs.spring.io/spring-ldap/docs/2.3.1.RELEASE/reference/#basic-authentication

...