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

通过注释而不是XML配置Spring LdapTemplate的最佳实践?

易祖鹤
2023-03-14

对于Spring Boot应用程序,我使用注释成功地配置了SpringLdapTemplate,包括应用程序中带有@Values的LdapContextSource依赖项。财产。(呜呜!我找不到一个例子,所以这可能会帮助其他人。)

下面的代码片段设置了上下文源,将其插入到LdapTemplate中,并将其自动连接到my DirectoryService中。

有没有更好/更干净的方法来设置ContextSource在Spring Boot应用程序中?

应用属性(在类路径上):

ldap.url=ldap://server.domain.com:389
ldap.base:OU=Employees,OU=Users,DC=domain,DC=com
ldap.username:CN=myuserid,OU=employees,OU=Users,DC=domain,DC=com
ldap.password:secretthingy

MyLdapContextSource.java:

@Component
public class MyLdapContextSource extends LdapContextSource implements ContextSource {

    @Value("${ldap.url}")
    @Override
    public void setUrl(String url) { super.setUrl(url);  }

    @Value("${ldap.base}")
    @Override
    public void setBase(String base) {super.setBase(base); }

    @Value("${ldap.username}")
    @Override
    public void setUserDn(String userDn) {super.setUserDn(userDn); }

    @Value("${ldap.password}")
    @Override
    public void setPassword(String password) { super.setPassword(password); }
}

我的模板。爪哇:

@Component
public class MyLdapTemplate extends LdapTemplate {

    @Autowired
    public MyLdapTemplate(ContextSource contextSource) { super(contextSource); }
}

目录服务。爪哇:

@Service
public class DirectoryService {

    private final LdapTemplate ldapTemplate;

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

    @Autowired
    public DirectoryService(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; }

    public Person lookupPerson(String username) {
        return (Person) ldapTemplate.lookup("cn=" + username, new PersonAttributesMapper());
    }

    public List<Person> searchDirectory(String searchterm) {
        SearchControls searchControls = new SearchControls();
        searchControls.setCountLimit(25);
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        List<Person> people = (List<Person>) ldapTemplate.search(
                BASE_DN, "cn=" + searchterm, searchControls, new PersonAttributesMapper());
        return people;
    }
}

共有2个答案

东方森
2023-03-14

对于直截了当的情况,根本不需要显式地连接LDAP。这正是Spring Boot首先要通过固执己见来消除的。

确保包含spring boot starter数据ldapspring ldap核心依赖项,例如在pom:xml中包含Maven:

xml prettyprint-override"><dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>

应用程序中配置LDAP。属性,使用以下键:

# Note the spring prefix for each and use just the CN for username
spring.ldap.url=ldap://server.domain.com:389
spring.ldap.base=OU=Employees,OU=Users,DC=domain,DC=com
spring.ldap.username=myuserid
spring.ldap.password=secretthingy

然后简单地依靠Spring自动连线,例如使用现场注射1

@Autowired
private final LdapTemplate ldapTemplate;

参考: Spring Boot参考指南: LDAP

1通常不推荐现场注射,但在这里用于简洁。

冯星阑
2023-03-14

为什么所有的子类?只需使用配置来配置bean。XML或Java配置。

@Configuration
public class LdapConfiguration {

    @Autowired
    Environment env;

    @Bean
    public LdapContextSource contextSource () {
        LdapContextSource contextSource= new LdapContextSource();
        contextSource.setUrl(env.getRequiredProperty("ldap.url"));
        contextSource.setBase(env.getRequiredProperty("ldap.base"));
        contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
        contextSource.setPassword(env.getRequiredProperty("ldap.password"));
        return contextSource;
    }

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

}

您的DirectoryService可以保持不变,因为它将自动连接LdapTemplate

一般的经验法则是,您不想扩展基础结构bean(比如DataSourceLdapTemplate),而是显式地配置它们。这与应用程序bean(服务、存储库等)不同。

 类似资料:
  • 问题内容: 对于Spring Boot应用程序,我使用 批注 成功配置了Spring ,其中包括application.properties中带有s 的依赖项。(糟糕!我找不到示例,所以也许这会对其他人有所帮助。) 片段(如下所示)设置了上下文源,将其注入,然后自动将其连接到我的DirectoryService中。 有没有更好/更干净的方法来在Spring Boot应用中进行设置? applica

  • 问题内容: 我已经使用Hibernate几年了,但仅将其与注释一起使用,并在代码中设置了连接参数。 我是否通过不使用XML文件来“缺少某些东西”?是否只有XML提供重要的功能?是否存在使用XML有意义的情况或模式? 问题答案: 我认为可以肯定地说您不会错过任何事情。 如果XML中有任何功能无法在属性中表示(我相信有一些罕见的情况),那么您仍然可以选择使用[RawXml]并将XML写入属性中。因此,

  • 我已经从SO问题中得到了在没有XML配置的情况下设置LdapContextSource和LdapTemplate的答案。通过注释而不是XML配置SpringLdap模板的最佳实践? 下面xml的注解方式应该是什么——基于接口自动创建存储库bean?

  • 有没有办法让通过注释加载工厂中仍然是的

  • 问题内容: 在最近我从事的一些大型项目中,选择其中一种(XML或注释)似乎变得越来越重要。随着项目的发展,一致性对于可维护性非常重要。 我的问题是:与基于注释的配置相比,基于XML的配置有哪些优势?与基于XML的配置相比,基于注释的配置有哪些优势? 问题答案: 注释有其用途,但它们不是杀死XML配置的灵丹妙药。我建议将两者混合! 例如,如果使用Spring,则将XML用于应用程序的依赖注入部分是完

  • 本文档旨在汇总和强调用户指南、快速开始文档和示例中的最佳实践。该文档会很活跃并持续更新中。如果你觉得很有用的最佳实践但是本文档中没有包含,欢迎给我们提Pull Request。 通用配置建议 定义配置文件的时候,指定最新的稳定API版本(目前是V1)。 在配置文件push到集群之前应该保存在版本控制系统中。这样当需要的时候能够快速回滚,必要的时候也可以快速的创建集群。 使用YAML格式而不是JSO