gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:08:09 28 4
gpt4 key购买 nike

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

片段(如下)设置上下文源,将其注入(inject) LdapTemplate,然后将其 Autowiring 到我的 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;
}
}

最佳答案

为什么是所有的子类?只需使用配置来配置 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 可以保持不变,因为它将 Autowiring LdapTemplate

一般的经验法则是,您不想扩展您的基础设施 bean(如 DataSourceLdapTemplate),而是明确地配置它们。这与您的应用程序 bean(服务、存储库等)相反。

关于java - 通过注释而不是 XML 配置 Spring LdapTemplate 的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25515345/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com