【问题标题】:Best practice for configuring Spring LdapTemplate via annotations instead of XML?通过注释而不是 XML 配置 Spring LdapTemplate 的最佳实践?
【发布时间】:2014-10-20 08:11:07
【问题描述】:

对于 Spring Boot 应用程序,我使用 注释 成功配置了 Spring LdapTemplate,包括来自 application.properties 的 LdapContextSource 依赖项和 @Values。 (Woot!我找不到示例,所以也许这对其他人有帮助。)

sn-ps(如下)设置上下文源,将其注入LdapTemplate,然后将其自动连接到我的 DirectoryService。

有没有更好/更简洁的方法在 Spring Boot 应用中设置 ContextSource

application.properties(在类路径上):

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); }
}

MyLdapTemplate.java:

@Component
public class MyLdapTemplate extends LdapTemplate {

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

DirectoryService.java:

@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;
    }
}

【问题讨论】:

  • 为什么?为什么要进行所有子类化?为什么不简单地在 xml 或 java config 中配置它们?仅配置的子类化对我来说似乎有点过分......
  • 谢谢你的建议,M! (也就是说,显然“为什么”的答案是我正在学习的 b/c 并且它有效,但这就是为什么我把它拿出来进行审查并提出更好/更清洁方式的建议。一个更友好的方式可能有一直以来,“为了更好/更清洁的方法,请使用 @Configuration 和方法创建一个配置类,这些方法返回您需要使用 Environment 来访问 Spring Boot 属性而不是 @Value 的 bean 类型。”——我会尝试代码并报告。

标签: java spring-boot annotations spring-ldap


【解决方案1】:

为什么是所有的子类?只需使用配置来配置 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(服务、存储库等)相反。

【讨论】:

  • 再次感谢,M - 您的建议很有效,减少了 LoC,并帮助我更好地理解基于注释的配置风格。
【解决方案2】:

对于直截了当的情况,根本不需要显式连接您的 LDAP。这是 Spring Boot 旨在通过固执己见来消除的那种东西。

确保包含 spring-boot-starter-data-ldapspring-ldap-core 依赖项,例如pom:xml 中的 Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>

使用以下键在 application.properties 中配置您的 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 Reference Guide: LDAP


1 字段注入一般为not recommended,但这里为了简洁起见。

【讨论】:

  • 自 2014 年以来对这个 Q 的重大更新。这些年来发生了很多变化。下次我使用 LDAP 时,将使用此信息。谢谢!
  • 这对我今天关于连接 LDAP 服务器的问题有所帮助。非常感谢您!
  • 知道如何使用上述方法加载两个 LDAP 服务器配置吗?
猜你喜欢
  • 2011-03-09
  • 2010-11-30
  • 2011-05-25
  • 2014-05-15
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
相关资源
最近更新 更多