【问题标题】:How to get user's full DN in Spring Data LDAP repositories如何在 Spring Data LDAP 存储库中获取用户的完整 DN
【发布时间】:2020-06-01 15:16:08
【问题描述】:

我正在为用户管理应用程序创建一个扩展,该应用程序使用 Spring Data Ldap 存储库维护 OpenLDAP 服务器中的用户以进行身份​​验证(除了内部数据库),因为该应用程序已经在使用 Spring 5 和 Spring Data 存储库。

基本的用户和组存储库用于查找、创建和更新,但有一个例外:组成员身份,因为成员属性需要完整的 DN(可分辨名称),不幸的是,用户存储库的 @Id 键是一个 相对 DN.

我的 Ldap 基础是“dc=example,dc=com” UserRepository 返回“cn=user1,ou=users” 但我需要完整的 DN:“cn=user1,ou=users,dc=example,dc=com”

基本名称从 ApplicationConfig.java 加载到 ContextSource 中,但由于存储库是动态生成的,并且我只定义了一个接口,我不知道如何注入该接口或属性并使用它编写代码。

我还没有找到任何返回完整 DN 的方法。我通过 this question 和来自 Spring LDAP 的 user admin example 找到了看起来像解决方案的方法。但是,这涉及回退到 Spring LDAP LdapTemplate,这是从 Spring Data LDAP 存储库到更通用的查询模型的一步。此外,由于依赖冲突,我无法让示例自行构建或运行,因此我担心混合 Spring Data LDAP 和 Spring Ldap 模板的解决方案的寿命。

我正在寻找的是从用户、组或其存储库类中检索完整 DN 或基本名称的任何方法,以便在将成员添加到组或查找组时提供这些成员。

【问题讨论】:

    标签: spring-data spring-ldap


    【解决方案1】:

    经过一些试验和许多错误,我找到了一种相当干净的方法来获取 Ldap 基本路径,将 ApplicationConfig 中定义的 ContextSource 也注入到 Service 类中。对于其他正在寻找这个的人:

    ApplicationConfig.java

    @Configuration
    @PropertySource("classpath:ldap.properties")
    
    ...
    
    @Value("${ldap.url}") String ldapUrl;
    @Value("${ldap.base}") String ldapBase;
    @Value("${ldap.user}") String ldapUser;
    @Value("${ldap.password}") String ldapPassword;
    
    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(ldapUrl);
        contextSource.setBase(ldapBase);
        contextSource.setUserDn(ldapUser);
        contextSource.setPassword(ldapPassword);
        return contextSource;
    }
    
    @Bean
    LdapTemplate ldapTemplate(ContextSource contextSource) {
        return new LdapTemplate(contextSource);
    }
    

    MyServiceImpl.java

    @Repository
    @Transactional
    public class MyServiceImpl implements MyService {
    
        @Autowired
        private LdapUserRepository userRepository;
    
        @Autowired
        private LdapGroupRepository groupRepository;
    
        @Autowired
        LdapContextSource contextSource;
    
        ...
    
        @Override
        public Collection<LdapGroup> findGroupByMember(Name name) {
            LdapName dName = LdapUtils.prepend(LdapUtils.removeFirst(name,
                contextSource.getBaseLdapName()), contextSource.getBaseLdapName());
    
            return groupRepository.findByMember(dName);
        }
    }
    

    借助 LdapUtils 方法删除和添加基本路径,我可以将任何传入名称清理为完整 DN。这使 LdapGroupRepository 保持简单,只有一个接口类,其余由 Spring Data LDAP 完成:

    LdapGroupRepository.java

    public interface LdapGroupRepository extends LdapRepository<LdapGroup> {
    
        Optional<LdapGroup> findByName(String groupName);
    
        @Query("(member={0})")
        Collection<LdapGroup> findByMember(Name member);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多