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

Spring护套 2 - 连接两个 LDAP 模板

许波涛
2023-03-14

我需要在我的Spring Boot 2应用程序中配置多个LDAP数据源/ LdapTemplates。第一个LdapTemplate将用于大部分工作,而第二个将用于偶尔出现的数据子集(存放在其他地方)。

我读过这些关于StackOverflow的问题,但它们似乎适用于Spring Boot 1。
一个Spring ldap存储库项目可以访问两个不同的ldap目录吗
具有Spring LDAP Repository的多个LDAP存储库

从我收集的信息来看,无论如何,大部分的配置/设置都必须完成,即使只是一个LDAP数据源,回到Spring Boot 1。对于Spring Boot 2,我只是把属性放在我的配置文件中,就像这样

ldap.url=ldap://server.domain.com:389
ldap.base:DC=domain,DC=com
ldap.username:domain\ldap.svc.acct
ldap.password:secret

并像这样自动连接存储库中的模板

@Autowired
private final LdapTemplate ldapTemplate;

我很乐意去。(参见:https://stackoverflow.com/a/53474188/3669288)

对于第二个 LDAP 数据源,我是否可以只添加“ldap2”的属性和配置元素即可完成(请参阅链接的问题)?还是添加此配置会导致Spring Boot 2的自动配置认为我正在覆盖它,所以现在我丢失了我的第一个LdapTemplate,这意味着我现在也需要显式配置它?
如果是这样,我是否需要配置所有内容,还是仅部分配置即可工作?例如,如果我添加上下文源配置并将其标记为@Primary(这适用于 LDAP 数据源吗?),我是否可以跳过将其显式分配给第一个 LdapTemplate?在相关的说明中,我是否仍然需要添加@EnableLdapRepositories注释,否则该注释将由Spring Boot 2自动配置?

TLDR:要连接第二个LDAP模板,我需要在Spring Boot 2中添加的最低配置是什么?

共有1个答案

顾泰平
2023-03-14

这把我在周末学到的东西作为我自己问题的答案。我仍然不是这方面的专家,所以我欢迎有经验的回答或评论。

首先,我仍然不确定我是否需要@EnableLdapRepositories注释。我还没有使用这些功能,所以我不能说没有它是否重要,或者Spring Boot 2是否仍在自动处理它。我怀疑Spring靴2是,但我不确定。

其次,Spring Boot的自动配置都是在任何用户配置之后发生的,例如我的代码配置了第二个LDAP数据源。自动配置使用几个条件注释来确定它是否运行,具体取决于上下文源或 LdapTemplate 的存在。
这意味着它看到我的“第二个”LDAP上下文源(条件只是上下文源bean存在,无论它的名称是什么或它使用什么属性),并跳过创建一个本身,这意味着我不再配置我的主数据源的那一部分。
它还将看到我的“第二个”LdapTemplate(同样,条件只是存在一个 LdapTemplate bean,无论它的名称是什么,或者它使用的上下文源或属性是什么),并跳过创建一个本身,所以我再次不再配置我的主数据源的那一部分。
不幸的是,这些条件意味着在这种情况下,两者之间都没有中间(例如,我可以手动配置上下文源,然后仍然允许LdapTemplate的自动配置)。因此,解决方案是要么在自动配置后运行我的配置,要么根本不利用自动配置,然后自己设置它们。

至于让我的配置在自动配置之后运行:唯一的办法是让我的配置本身成为自动配置,并指定它的顺序在Spring的内置自动配置之后(见:https://stack overflow . com/a/53474188/3669288)。这不适合我的用例,所以对于我的情况(因为Spring Boot的设置对于标准的单源情况是有意义的)我坚持放弃自动配置并自己设置它们。

正如我的问题中所链接的,设置两个数据源在下面的两个回答中有很好的介绍(虽然部分是因为其他原因),但是我也将在这里详细介绍我的设置。< br > spring ldap存储库项目可以访问两个不同的LDAP目录吗?< br >带有Spring LDAP存储库的多个LDAP存储库

首先,需要创建配置类,因为以前在Spring Boot 2中根本不需要配置类。同样,我省略了@EnableLdapRepositories注释,部分原因是我还没有使用它,部分原因是我认为Spring Boot 2仍然会为我涵盖它。(注意:所有这些代码都是在“堆栈溢出”答案框中键入的,因为我没有编写此代码的开发环境,因此跳过了导入,并且代码可能无法完全编译和正常运行,尽管我希望它很好。

@Configuration
public class LdapConfiguration {
}

其次是手动配置主数据源;过去是自动配置的,但现在不再是了。这里可以利用Spring Boot的自动配置的一个部分,那就是它读取标准spring.ldap.*属性(到属性对象中),但是因为没有给它命名,所以您必须用它的完全限定类名来引用它。这意味着您可以直接跳过为主数据源设置上下文源。这段代码不像实际的自动配置代码那样功能齐全(参见:Spring Code),我将此LdapTemplate标记为@主,因为对于我的使用,这是主要数据源,因此它是所有其他自动装配调用的默认值。这也意味着您不需要自动装配此源的@Qualifier(如后所述)。

@Configuration
public class LdapConfiguration {
    @Bean(name="contextSource")
    public LdapContextSource ldapContextSource(@Qualifier("spring.ldap-org.springframework.boot.autoconfigure.ldap.LdapProperties") LdapProperties properties) {
        LdapContextSource source = new LdapContextSource();
        source.setUrls(properties.getUrls());
        source.setUserDn(properties.getUsername());
        source.setPassword(properties.getPassword());
        source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment()));
        
        return source;
    }
    
    @Bean(name="ldapTemplate")
    @Primary
    public LdapTemplate ldapTemplate(@Qualifier("contextSource") LdapContextSource source) {
        return new LdapTemplate(source);
    }
}

第三是手动配置辅助数据源,这是导致所有这些开始的原因。对于这个,您确实需要配置将属性读取到LdapProperties对象中。此代码建立在之前的代码之上,因此您可以查看上下文的完整类。

@Configuration
public class LdapConfiguration {
    @Bean(name="contextSource")
    public LdapContextSource ldapContextSource(@Qualifier("spring.ldap-org.springframework.boot.autoconfigure.ldap.LdapProperties") LdapProperties properties) {
        LdapContextSource source = new LdapContextSource();
        source.setUrls(properties.getUrls());
        source.setUserDn(properties.getUsername());
        source.setPassword(properties.getPassword());
        source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment()));
        
        return source;
    }
    
    @Bean(name="ldapTemplate")
    @Primary
    public LdapTemplate ldapTemplate(@Qualifier("contextSource") LdapContextSource source) {
        return new LdapTemplate(source);
    }
    
    
    
    @Bean(name="ldapProperties2")
    @ConfigurationProperties("app.ldap2")
    public LdapProperties ldapProperties2() {
        return new LdapProperties();
    }
    
    @Bean(name="contextSource2")
    public LdapContextSource ldapContextSource2(@Qualifier("ldapProperties2") LdapProperties properties) {
        LdapContextSource source = new LdapContextSource();
        source.setUrls(properties.getUrls());
        source.setUserDn(properties.getUsername());
        source.setPassword(properties.getPassword());
        source.setBaseEnvironmentProperties(Collections.unmodifiableMap(properties.getBaseEnvironment()));
        
        return source;
    }
    
    @Bean(name="ldapTemplate2")
    public LdapTemplate ldapTemplate2(@Qualifier("contextSource2") LdapContextSource source) {
        return new LdapTemplate(source);
    }
}

最后,在使用这些LdapTemplates的类中,您可以像平常一样自动连接它们。这使用构造函数自动连接,而不是其他两个答案使用的字段自动连接。尽管建议使用构造器自动布线,但这两种方法在技术上都是有效的。

@Component
public class LdapProcessing {
    protected LdapTemplate ldapTemplate;
    protected LdapTemplate ldapTemplate2;
    
    @Autowired
    public LdapProcessing(LdapTemplate ldapTemplate, @Qualifier("ldapTemplate2") LdapTemplate ldapTemplate2) {
        this.ldapTemplate = ldapTemplate;
        this.ldapTemplate2 = ldapTemplate2;
    }
}

TLDR:定义“第二个”LDAP数据源会停止第一个LDAP数据源的自动配置,所以如果使用多个数据源,那么两个数据源都必须(几乎完全)手动配置;Spring的自动配置甚至不能用于第一个LDAP数据源。

 类似资料:
  • 我正在努力使用jpa Crudepository接口连接两个实体模型。我不知道如何映射两个实体模型并在@query注释中编写查询。这些是我的实体类。我想执行这个查询“从taxi\u driver\u映射中选择dppd.payment\u plan\u id,dppd.attribute\u value,dppd.attribute\u id作为tdm加入driver\u payment\u pla

  • 我收到了以下LDAP参数,但不确定如何在PHP中建立连接。我不确定每一组参数使用哪一个PHP函数。以下是我得到的参数: 服务器:ldaps://the_server.com:636 dc=the_info,dc=more_info,dc=com 用户搜索库:ou=公司用户 用户搜索筛选器:sAMAccountName={0} 组搜索库:OU=Security,OU=companygroup 组搜索

  • 这样使用是创建1个连接还是2个啊? 这样使用是创建1个连接还是2个啊?

  • 问题内容: 有简单的解决方案,可通过串联两个或java 。由于是经常使用的。是否有任何简单的方法来连接两个? 这是我的想法: 它可以工作,但实际上可以转换为,然后再次转换回。 问题答案: 您可以使用协力让这件事没有任何自动装箱拆箱或完成。这是它的外观。 请注意,返回,然后将其与另一个串联,然后再收集到数组中。 这是输出。 [1、34、3、1、5]

  • 行动时刻 - 将FreeRADIUS连接到LDAP 以下部分将向您展示如何将FreeRADIUS连接到LDAP。 安装slapd 确保在Linux服务器上安装了slapd。下表可用作在本书中讨论的三个发行版中的每一个上安装slapd的指南: 发行版 用于安装MySQL服务器的命令 CentOS yum install openldap-servers openldap-clients SUSE z

  • 问题内容: 我有两个表,如下所示: 我想列出参加活动17的所有人(包括学生和教师)的名字。无论如何,我可以获得以下结果: 无需创建新表(仅使用表达式或派生关系的嵌套)? 在actid上加入JOIN会得到如下结果: 我想我需要一种串联形式? 问题答案: 您可能(或可能不需要)对ID不唯一的内容进行处理,例如