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

Ldap 查询 - 使用Spring启动进行配置

沙岳
2023-03-14

我有一个需要执行LDAP查询的Spring启动应用程序。我正在尝试从Spring启动文档中获取以下建议:

Internet上已经发布了许多使用XML配置的Spring配置示例。如果可能,请始终尝试使用等效的基于Java的配置。

在Spring XML配置文件中,我会使用:

 <ldap:context-source
          url="ldap://localhost:389"
          base="cn=Users,dc=test,dc=local"
          username="cn=testUser"
          password="testPass" />

   <ldap:ldap-template id="ldapTemplate" />

   <bean id="personRepo" class="com.llpf.ldap.PersonRepoImpl">
      <property name="ldapTemplate" ref="ldapTemplate" />
   </bean>

我如何使用基于Java的配置来配置它?我需要能够更改ldap的URL、base、用户名和密码属性:上下文源,而无需代码重建。

共有1个答案

斜博超
2023-03-14

<代码>

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    @ConfigurationProperties(prefix="ldap.contextSource")
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        return contextSource;
    }

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

    @Bean 
    public PersonRepoImpl personRepo(LdapTemplate ldapTemplate) {
        PersonRepoImpl personRepo = new PersonRepoImpl();
        personRepo.setLdapTemplate(ldapTemplate);
        return personRepo;
    }
}

为了允许您在不重新生成代码的情况下更改配置,我使用了Spring Boot的@ConfigurationProperties。这将在应用程序的环境中查找以 ldap.context 为前缀的属性,然后通过调用匹配的 setter 方法将它们应用于 LdapContextSource Bean。若要应用问题中的配置,可以使用具有四个属性的 application.properties 文件:

ldap.contextSource.url=ldap://localhost:389
ldap.contextSource.base=cn=Users,dc=test,dc=local
ldap.contextSource.userDn=cn=testUser
ldap.contextSource.password=testPass
 类似资料:
  • 我正在尝试从Spring Boot应用程序连接到Microsoft SQL Server 2016。SQL server是使用windows身份验证配置的。 以下是配置 注意:我正在使用Windows身份验证。 谁能帮忙吗?

  • 问题内容: 我有一个关于使用JdbcTemplate进行动态查询的问题。 我的代码如下: 现在,我的问题是,我想要与插入查询中的自动生成问号相同数量的“值”。 现在,值变量考虑为一个字符串,因此,如果我有2个或更多问号,则在值变量中只有一个用逗号分隔的完整字符串,因此它不起作用。 见下面我的查询: 我想要如下: 问题答案: }

  • 问题内容: 我有一个名为“ acts”的表,该表具有3个被索引在一起的列: 在表格中,一行有一个名为“红色暴动”的行为 当我执行以下搜索时,结果中会出现红色暴动: 但是,如果我对此进行扩展并搜索,则不会返回任何结果: 这是为什么?有没有更好的方法在查询中使用通配符? 我尝试更改为,但这在任一查询上均未返回任何结果。 问题答案: 值“ re”是MATCH()搜索的停用词。 http://dev.my

  • 我想使用条件查询。 这是我的问题 这是我的java代码 它给出

  • 我正在为spring批处理使用java配置(spring boot)。我有一个员工Id列表,对于每个Id,我需要运行一个查询(如下所示),然后处理数据。 我知道我们可以使用阅读器。setPreparedStatementSetter动态设置上述SQL中的参数。但是,我不确定如何对列表中的每个员工id重复批处理过程。即使我将reader()标记为@StepScope,也只会调用一次reader。(即

  • 我想使用返回对象的注释在spring boot中编写一个查询。 所以,我的问题是:分页的最佳JPA查询类型(JPQL,NativeQuery,...)是什么? 提前谢谢你!