Di postingan sebelumnya saya pernah menjelaskan tentang LDAP. Kali ini saya mencoba memanfaatkan LDAP ke dalam project yang saya kembangkan. Saya menggunakan Spring LDAP sebagai frameworknya, Apache Directory Server sebagai server untuk LDAP dan Apache Directory Studio yang merupakan plugins di eclipse (IDE yang saya gunakan) untuk editor LDIF. Berikut ini file2 yang diperlukan.

Kelas LDAPAccessor berguna untuk menangani kerja LDAP.

public class LDAPAccessor {
 private LdapTemplate ldapTemplate;

public boolean authenticate(String username, String password) {
 LdapContextSource ctxSource = new LdapContextSource();
 ctxSource.setUrl(PropertyLooker.get("ldap.url"));
 ctxSource.setUserName(buildDn(username) + "," + PropertyLooker.get("ldap.base"));
 ctxSource.setPassword(password);
 ctxSource.setPooled(false);
 System.out.println(buildDn(username) + "," + PropertyLooker.get("ldap.base"));
 try {
 ctxSource.afterPropertiesSet();
 ctxSource.getReadWriteContext();
 return true;
 } catch (Exception e) {
 System.out.println(e);
 return false;
 }
 }

@Override
 public void update(User user) {
 super.update(user);

Attributes personAttributes = new BasicAttributes();
 BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
 personBasicAttribute.add("inetOrgPerson");
 personBasicAttribute.add("organizationalPerson");
 personBasicAttribute.add("person");
 personBasicAttribute.add("top");
 personAttributes.put(personBasicAttribute);
 personAttributes.put("cn", user.getUsername());
 personAttributes.put("uid", user.getUsername());
 personAttributes.put("givenname", user.getName().getFirst());
 personAttributes.put("sn", user.getName().getLast());
 personAttributes.put("userPassword", new StringUtils().decodeBase64(user.getPassword()));
 ldapTemplate.rebind(buildDn(userEdited), null, personAttributes);
 }

public String buildDn(String username){
 return PropertyLooker.get("ldap.bind.filter").replaceAll("%s", username);
 }

public String buildDn(User user){
 return buildDn(user.getUsername());
 }

public void setLdapTemplate(LdapTemplate ldapTemplate) {
 this.ldapTemplate = ldapTemplate;
 }

}

Method authenticate() akan bekerja pada saat melakukan autentikasi dalam halaman login. Sedangkan method update(User user) bekerja pada saat mengedit detail dari atribut dalam LDAP, misalnya yang saya gunakan disini adalah cn, uid, givername, sn, dan userPassword. Untuk atribut uid harus diisi sesuai dengan username di dalam database.

Konfigurasi pada applicationContext-LDAP.xml yang saya gunakan sebagai berikut.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="contextSource">
 <property name="url" value="ldap://localhost:10389" />
 <property name="base" value="ou=users,ou=system" />
 <property name="userName" value="uid=admin,ou=system" />
 <property name="password" value="secret" />
 </bean>
 <bean id="ldapTemplate" class="org.springframework.ldap.LdapTemplate">
 <constructor-arg ref="contextSource" />
 </bean><bean id="userAccessor" class="org.myproject.accessor.LDAPAccessor" singleton="false">
 <property name="ldapTemplate" >
 <ref bean="ldapTemplate" />
 </property>
 <property name="persistenceManager">
 <ref bean="persistenceManager" />
 </property>
</bean>

Nb: Untuk cara penambahan user dalam Directory LDAP melalui Apache Directory Studio dapat dilihat di sini.

Advertisement