当前位置: 首页 > 知识库问答 >
问题:

具有Spring LDAP 存储库的多个 LDAP 存储库

祁承嗣
2023-03-14

我想使用Spring LDAP 设置多个 LDAP 存储库。我的目标是同时在所有存储库中创建或更新对象。

我使用LdapRepository Spring接口,我认为目前这是不可能的。

我想知道我是否可以创建自己的LdapRepository来扩展Spring,但是我不知道如何开始。

这是我的配置:

@Configuration
@EnableLdapRepositories("com.xxx.repository.ldap")
@PropertySource("classpath:ldap.properties")
public class LdapConfiguration {

    @Autowired
    Environment ldapProperties;

    @Bean
    public LdapContextSourceCustom contextSourceTarget() {
        LdapContextSourceCustom ldapContextSource = new LdapContextSourceCustom();
        ldapContextSource.setUrl(ldapProperties.getProperty("ldap.url"));
        ldapContextSource.setBase(ldapProperties.getProperty("ldap.base"));
        ldapContextSource.setUserDn(ldapProperties.getProperty("ldap.userDn"));
        ldapContextSource.setPassword(ldapProperties.getProperty("ldap.password"));
        ldapContextSource.setKeyStoreFile(ldapProperties.getProperty("ldap.truststore"));

        return ldapContextSource;
    }

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

完整地说,一个存储库:

public interface LdapUserRepository extends LdapRepository<LdapUser> {

}

知道怎么做吗?

提前感谢任何帮助。

共有3个答案

晏富
2023-03-14

现在实际上有一种更简单的方法:

创建具有相应属性的@EnableLdapRepositories指定的多个配置

创建第一个配置

@Configuration
@EnableLdapRepositories(basePackages = "first.ldap.package.repository.**", ldapTemplateRef = "firstLdapTemplate")
public class FirstLDAPConfig {
   ....detail


    @Bean("firstLdapTemplate")
    public LdapTemplate firstLdapTemplate() {
        ...template creation
    }

}

创建第二个配置

@Configuration
@EnableLdapRepositories(basePackages = "second.ldap.package.repository.**", ldapTemplateRef = "secondLdapTemplate")
public class SecondLDAPConfig {
   ....detail

    @Bean("secondLdapTemplate")
    public LdapTemplate secondLdapTemplate() {
        ...template creation
    }
}

每个配置都应处理自己的上下文来源,然后只有“启用演示文稿”注释中的指定存储库才会使用该特定的上下文源和 Ldap 模板

洪胤
2023-03-14

我不知道我是否理解正确,但这是我们所做的:

> < li>

全局配置类

@Bean("odm")
public ObjectDirectoryMapper odm() {
    return new DefaultObjectDirectoryMapper();
};

第一个配置类

@Configuration
@PropertySource("classpath:ldap-one.properties")
public class LdapOneConfiguration {

    @Autowired
    Environment ldapProperties;

    @Bean(name = "contextSourceOne")
    public LdapContextSourceCustom contextSourceLdapOneTarget() {
        LdapContextSourceCustom ldapContextSource = new LdapContextSourceCustom();
        ldapContextSource.setUrl(ldapProperties.getProperty("ldap-one.url"));
        ldapContextSource.setBase(ldapProperties.getProperty("ldap-one.base"));
        ldapContextSource.setUserDn(ldapProperties.getProperty("ldap-one.userDn"));
        ldapContextSource.setPassword(ldapProperties.getProperty("ldap-one.password"));
        ldapContextSource.setKeyStoreFile(ldapProperties.getProperty("ldap-one.truststore"));

        return ldapContextSource;
    }

    @Bean(name = "ldapTemplateOne")
    public LdapTemplate ldapOneTemplate(@Qualifier("contextSourceOne") LdapContextSourceCustom contextSource) {
        return new LdapTemplate(contextSource);
    }

    @Bean(name = "ldapUserRepoOne")
    public LdapUserRepository ldapUserRepositoryOne(@Qualifier("ldapTemplateOne") LdapTemplate ldapTemplate,
        @Qualifier("odm") ObjectDirectoryMapper odm) {
        return new LdapUserRepository(ldapTemplate, odm);
    }

    @Bean(name = "ldapFamilyRepoOne")
    public LdapFamilyRepository ldapFamilyRepositoryOne(@Qualifier("ldapTemplateOne") LdapTemplate ldapTemplate,
        @Qualifier("odm") ObjectDirectoryMapper odm) {
        return new LdapFamilyRepository(ldapTemplate, odm);
    }
}

第二个LDAP配置类

@Configuration
@PropertySource("classpath:ldap-two.properties")
public class LdapTwoConfiguration {
    @Autowired
    Environment ldapProperties;

    @Bean(name = "contextSourceTwo")
    public LdapContextSourceCustom contextSourceLdapTwoTarget() {
        LdapContextSourceCustom ldapContextSource = new LdapContextSourceCustom();
        ldapContextSource.setUrl(ldapProperties.getProperty("ldap-two.url"));
        ldapContextSource.setBase(ldapProperties.getProperty("ldap-two.base"));
        ldapContextSource.setUserDn(ldapProperties.getProperty("ldap-two.userDn"));
        ldapContextSource.setPassword(ldapProperties.getProperty("ldap-two.password"));
        ldapContextSource.setKeyStoreFile(ldapProperties.getProperty("ldap-two.truststore"));

        return ldapContextSource;
    }

    @Bean(name = "ldapTemplateTwo")
    public LdapTemplate ldapTwoTemplate(@Qualifier("contextSourceTwo") LdapContextSourceCustom contextSource) {
        return new LdapTemplate(contextSource);
    }

    @Bean(name = "ldapUserRepoTwo")
    public LdapUserRepository ldapUserRepositoryTwo(@Qualifier("ldapTemplateTwo") LdapTemplate ldapTemplate,
        @Qualifier("odm") ObjectDirectoryMapper odm) {
        return new LdapUserRepository(ldapTemplate, odm);
    }

    @Bean(name = "ldapFamilyRepoTwo")
    public LdapFamilyRepository ldapFamilyRepositoryTwo(@Qualifier("ldapTemplateTwo") LdapTemplate ldapTemplate,
        @Qualifier("odm") ObjectDirectoryMapper odm) {
        return new LdapFamilyRepository(ldapTemplate, odm);
    }

}

LdapUser存储库

public class LdapUserRepository extends SimpleLdapRepository<LdapUser> {

    public LdapUserRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm) {
        super(ldapOperations, odm, LdapUser.class);
    }
}

LdapFamily知识库

public class LdapFamilyRepository extends SimpleLdapRepository<LdapFamily> {

    public LdapFamilyRepository(LdapOperations ldapOperations, ObjectDirectoryMapper odm) {
        super(ldapOperations, odm, LdapFamily.class);
    }
}

LdapUser服务(与LdapFamily服务相同)

@Service
public class LdapUserServiceImpl implements LdapUserService {

    @Autowired
    private ApplicationContext appContext;

    private LdapUserRepository uniqueLdapUserRepo;

    private List<LdapUserRepository> ldapUserRepoList;

    @PostConstruct
    private void setUniqueRepo() {
        uniqueLdapUserRepo = appContext.getBeansOfType(LdapUserRepository.class).values().iterator().next();
        ldapUserRepoList = new ArrayList<>(appContext.getBeansOfType(LdapUserRepository.class).values());
    }

    @Override
    public LdapUser getUser(String uid) {
        return uniqueLdapUserRepo.findOne(query().where("uid").is(uid));
    }

    @Override
    public void saveUser(LdapUser user) {
        for(LdapUserRepository repo: ldapUserRepoList){
            repo.save(user);
    }
}

}

我们删除了LDAP存储库的自动配置:

@EnableLdapRepositories(basePackages = "com.afklm.paul.repository.ldap", ldapTemplateRef = "ldapTwoTemplate")

谢谢ryan2049的帮助。

方谦
2023-03-14

1)可以指定多个LDAP存储库配置。请参阅以下示例。[注意:这取决于spry-boot库]

@Configuration
@EnableLdapRepositories("com.xxx.repository.ldap")
@EnableConfigurationProperties(LdapProperties.class)
public class LdapConfiguration {

    @Autowired
    private Environment environment;

    @Bean(name="contextSource1")
    public LdapContextSource contextSourceTarget(LdapProperties ldapProperties) {
        LdapContextSource source = new LdapContextSource();
        source.setUserDn(this.properties.getUsername());
        source.setPassword(this.properties.getPassword());
        source.setBase(this.properties.getBase());
        source.setUrls(this.properties.determineUrls(this.environment));
        source.setBaseEnvironmentProperties(Collections.<String,Object>unmodifiableMap(this.properties.getBaseEnvironment()));
        return source;
    }

    @Bean
    public LdapTemplate ldapTemplate(@Qualifier("contextSource1") LdapContextSource contextSource){
        return new LdapTemplate(contextSource);
    }
}

您可以使用Spring。应用程序中的ldap前缀。属性以配置上述LdapConfiguration。您可以通过签出来查看可用属性https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/LdapProperties.java.

@Configuration
@EnableLdapRepositories(basePackages="com.yyy.repository.ldap", ldapTemplateRef="ldapTemplate2")
public class LdapConfiguration2 {

    @Autowired
    private Environment environment;

    @Bean(name="ldapProperties2")
    @ConfigurationProperties(prefix="spring.ldap2")
    public LdapProperties ldapProperties() {
        return new LdapProperties();
    }

    @Bean(name="contextSource2")
    public LdapContextSource contextSourceTarget(@Qualifier("ldapProperties2") LdapProperties ldapProperties) {
        LdapContextSource source = new LdapContextSource();
        source.setUserDn(this.properties.getUsername());
        source.setPassword(this.properties.getPassword());
        source.setBase(this.properties.getBase());
        source.setUrls(this.properties.determineUrls(this.environment));
        source.setBaseEnvironmentProperties(Collections.<String,Object>unmodifiableMap(this.properties.getBaseEnvironment()));
        return source;
    }

    @Bean(name="ldapTemplate2")
    public LdapTemplate ldapTemplate(@Qualifier("contextSource2") LdapContextSource contextSource){
        return new LdapTemplate(contextSource);
    }
}

LdapConfiguration2将由Spring配置。application.properties中的ldap2前缀。

2)我不认为扩展存储库是解决方案。我建议创建一个@Service方法,用于循环访问存储库并应用更新。我将在下面提供两种方法。

实施例1)

@Service
public class UpdateRepositories {
    public void updateAllRepositories(LdapUserRepository userRepository1, LdapUserRepository userRepository2) {
        // apply updates to userRepository1 and userRepository2
    }
}

例2)

@Service
public class UpdateRepositories {
    public void updateAllRepositories(ApplicationContext appContext) {
        Map<String, LdapRepository> ldapRepositories = appContext.getBeansofType(LdapRepository.class)
        // iterate through map and apply updates
    }
}

我还没有编译过这段代码,所以如果有什么问题,或者您需要额外的指导,请告诉我。

 类似资料:
  • 如果我在一个gradle构建中指定了多个maven存储库,我希望如果gradle在其中一个存储库中找不到依赖项,它应该尝试其他存储库。相反,它在第一个存储库(内部公司nexus repo)上失败,因为那里不存在依赖项,并且构建失败 失败:构建失败,出现异常。 *问题:无法解析配置“:metadata security:compile”的所有依赖项。

  • 问题内容: 我想使用两个Git存储库构建一个项目。其中一个包含源代码,而另一个包含构建和部署脚本。 我的问题是我需要一个用于构建和部署项目不同部分(大型项目,多个存储库,相同的构建和部署脚本)的存储库,但是Jenkins似乎无法处理此问题(或者我不知道)不知道/找不到方法)。 问题答案: 更新 现在不建议使用多个SCM插件,因此用户应该迁移到Pipeline插件。 旧答案 是的,詹金斯可以处理。只

  • 搜索仓库和镜像 你可以使用 Docker 来搜索所有公开可用的仓库和镜像。 $ docker search ubuntu 这将通过 Docker 提供的关键字匹配来显示您可用的仓库列表。 私有仓库将不会显示到仓库搜索结果上。你可以通过 Docker Hub 的简况页面来查看仓库的状态。 仓库 你的 Docker Hub 仓库有许多特性。 stars 你的仓库可以用星被标记,你也可以用星标记别的仓

  • 这很可能通过Nexus配置得到解决。 我们使用maven进行hadoop开发。Nexus被配置为所有存储库的镜像,存储库被添加到Nexus公共组中。(参见Nexus中有没有更好的配置存储库的方法?) 我发现hadoop-core工件版本1.0.4显示来自spring-roo-repositoryhttp://spring-roo-repository.springsource.org/releas

  • 我正在寻找一个工具(最好是开源的)来与JackRabbit合作。例如,我想备份/恢复存储库的一些分支。有什么建议吗?

  • 我正在尝试将通用Jpa规范与Spring启动一起使用,但出现了这个问题。 在我的代码中,我试图使用模块概念,所以我有5个模块(实体、dao、服务、web和前端,带角度),所以这是我的代码: 我的通用Jpa规范接口。 存储库示例。 服务 和控制器