问题描述
对于 Spring Boot 应用程序,我使用 annotations 成功配置了 Spring LdapTemplate
,包括 LdapContextSource
依赖项和 @Value
s 来自 application.properties.(哇!我找不到例子,所以也许这会帮助其他人.)
For a Spring Boot application, I successfully configured a Spring LdapTemplate
using annotations, including the LdapContextSource
dependency with @Value
s from application.properties. (Woot! I couldn't find an example, so maybe this will help others.)
片段(如下)设置上下文源,将其注入LdapTemplate
,并将其自动装配到我的 DirectoryService.
The snippets (below) setup the context source, inject it into an LdapTemplate
, and autowire that into my DirectoryService.
是否有更好/更简洁的方法在 Spring Boot 应用中设置 ContextSource
?
application.properties(在类路径上):
application.properties (on the classpath):
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:
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:
MyLdapTemplate.java:
@Component
public class MyLdapTemplate extends LdapTemplate {
@Autowired
public MyLdapTemplate(ContextSource contextSource) { super(contextSource); }
}
DirectoryService.java:
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 配置.
Why all the subclasses? Just use configuration to configure the beans. Either XML or Java Config.
@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
.
Your DirectoryService
can remain the same as it will have the LdapTemplate
autowired.
一般的经验法则是,您不想扩展您的基础架构 bean(如 DataSource
或 LdapTemplate
),而是明确地配置它们.这与您的应用程序 bean(服务、存储库等)相反.
A general rule of thumb is that you don't want to extend your infrastructure beans (like DataSource
or LdapTemplate
) but configure them explicitly. This as opposed to your application beans (services, repositories etc.).
这篇关于通过注释而不是 XML 配置 Spring LdapTemplate 的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!