Несколько ldapTemplate не работает в ldapRepository Spring ldap - PullRequest
0 голосов
/ 11 марта 2020

Я работаю над проектом Spring ldap. Я также использую его, но когда я хочу иметь второй ldapTemplate для класса объекта User, он не работает!

вот пример кода:

//first ldapConfig
@Configuration
public class LdapConfiguration  {

    @Value("${spring.ldap.urls}")
    private String url;

    @Value("${spring.ldap.username}")
    private String userName;

    @Value("${spring.ldap.base}")
    private String base;

    @Value("${spring.ldap.password}")
    private String password;

    @Bean
    public LdapContextSource contextSource(){
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setBase(base);
        contextSource.setPassword(password);
        contextSource.setUrl(url);
        contextSource.setUserDn(userName);
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate(LdapContextSource ldapContextSource){
        return new LdapTemplate(ldapContextSource);
    }


//second ldapConfiguration
@Configuration
@EnableLdapRepositories(basePackages = "com.example.batch.repository", ldapTemplateRef="userLdapTemplate")
public class SecondLdapConfiguration  {

    @Value("${spring.ldap.urls}")
    private String url;

    @Value("${spring.ldap.username}")
    private String userName;

    @Value("${spring.ldap.base}")
    private String base;

    @Value("${spring.ldap.password}")
    private String password;

    @Bean
    public LdapContextSource contextSource(){
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setBase(base);
        contextSource.setPassword(password);
        contextSource.setUrl(url);
        contextSource.setUserDn(userName);
        return contextSource;
    }

    @Bean("userLdapTemplate")
    public LdapTemplate userLdapTemplate(LdapContextSource ldapContextSource){
        return new LdapTemplate(ldapContextSource);
    }


//UserLdapRepository
@Repository
public interface UserLdapRepository extends LdapRepository<User> {

    List<User> findAllByName(String name);
}

//User Object class
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entry(objectClasses = {"person","top","dcObject","organization"},base = "dc=users")
public final class  User {

    @Id
    private Name dn;

    @Attribute(name = "cn")
    private String name;

........some details

}

Когда я вызываю этот метод в таком классе, это ldapTemplate имеет значение, но findAllByName () возвращает 0 !!!! Если я удаляю первый ldapConfigUration и это ldapTemplate, findAllByName () для пользователя работает хорошо !! Пожалуйста, помогите мне! Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...