Tim*_*Tim 29 java annotations spring-ldap spring-boot
对于Spring Boot应用程序,我LdapTemplate使用注释成功配置了Spring ,包括LdapContextSource与@Valueapplication.properties中的s 的依赖关系.(哇!我找不到一个例子,所以也许这会帮助别人.)
片段(下面)设置上下文源,将其注入LdapTemplate到我的DirectoryService中,然后自动装入到我的DirectoryService中.
是否有更好/更清晰的方法来设置ContextSourceSpring Boot应用程序?
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
Run Code Online (Sandbox Code Playgroud)
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); }
}
Run Code Online (Sandbox Code Playgroud)
MyLdapTemplate.java:
@Component
public class MyLdapTemplate extends LdapTemplate {
@Autowired
public MyLdapTemplate(ContextSource contextSource) { super(contextSource); }
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
M. *_*num 47
为什么所有的子类?只需使用配置来配置bean.XML或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());
}
}
Run Code Online (Sandbox Code Playgroud)
你DirectoryService可以保持与LdapTemplate自动装配相同的效果.
一般的经验法则是,您不希望扩展基础结构bean(如DataSource或LdapTemplate),而是显式配置它们.这与您的应用程序bean(服务,存储库等)相反.
对于直截了当的情况,根本不需要显式连接 LDAP。这是 Spring Boot 旨在通过自以为是首先消除的那种事情。
确保您包含spring-boot-starter-data-ldap或spring-ldap-core依赖项,例如在您的 Maven 中pom:xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
然后简单地依靠 Spring 自动装配,例如使用字段注入1:
@Autowired
private final LdapTemplate ldapTemplate;
Run Code Online (Sandbox Code Playgroud)
1通常不推荐使用字段注入,但为了简洁起见,这里使用它。
| 归档时间: |
|
| 查看次数: |
32897 次 |
| 最近记录: |