8 Ocak 2020 Çarşamba

SpringLDAP LdapContextSource Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.ldap.core.support.LdapContextSource;
Açıklaması şöyle.
ContextSource is used for creating the LdapTemplate.
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.ldap</groupId>
  <artifactId>spring-ldap-core</artifactId>
</dependency>
constructor

Elimizde application.yml dosyası olsun.
ldap:
 contextSource:
   url: ldap://192.168.113:389 #Local
   base: dc=test,dc=test
   userDn: cn=admin,dc=test,dc=test
   password: test2019!@
Şöyle yaparız.
@Configuration
public class LdapConfiguration {
  @Bean
  @ConfigurationProperties(prefix="ldap.context-source")
  public LdapContextSource contextSource() {
    return new LdapContextSource();
  }

 @Bean
 public ContextSource poolingLdapContextSource() {
   PoolingContextSource pcs = new PoolingContextSource();
   pcs.setDirContextValidator(new DefaultDirContextValidator());
   pcs.setContextSource(contextSource());

  TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(pcs);

   return proxy;
  }
  // other configs like ldaptemplate
}
setBase metodu
Şöyle yaparız.
@Bean
public ContextSource contextSource() {
  LdapContextSource contextSource = new LdapContextSource();

  contextSource.setUrl("ldap://...");
  contextSource.setBase("dc=test,dc=test");
  contextSource.setUserDn("cn=admin,dc=test,dc=test");
  contextSource.setPassword("test2019!@");
  contextSource.afterPropertiesSet();

  PoolingContextSource pcs = new PoolingContextSource();
  pcs.setDirContextValidator(new DefaultDirContextValidator());
  pcs.setContextSource(contextSource);

  TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(pcs);

  return proxy;
}
setUrl metodu
Şöyle yaparız.
contextSource.setUrl("ldap://192.168.113.12:389");

Hiç yorum yok:

Yorum Gönder