Skip to content

Commit

Permalink
Use DirContextAdapter for binding new users
Browse files Browse the repository at this point in the history
  • Loading branch information
Fairuz Wan Ismail committed Dec 26, 2015
1 parent 22a6ae8 commit 3ac2dcd
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/main/java/sample/domain/LdapUserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ldap.NameAlreadyBoundException;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.ldap.core.support.AbstractContextMapper;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -65,19 +64,19 @@ public User doMapFromContext(DirContextOperations context) {
}
}

private Attributes buildAttributes(User user) {
Attributes attrs = new BasicAttributes();
BasicAttribute ocattr = new BasicAttribute("objectclass");
ocattr.add("top");
ocattr.add("person");
ocattr.add("inetOrgPerson");
ocattr.add("organizationalPerson");
attrs.put(ocattr);
attrs.put("cn", user.getFullName());
attrs.put("sn", user.getLastName());
attrs.put("userPassword", ldapShaPasswordEncoder.encodePassword(user.getPassword(), null));
return attrs;
}
// private Attributes buildAttributes(User user) {
// Attributes attrs = new BasicAttributes();
// BasicAttribute ocattr = new BasicAttribute("objectclass");
// ocattr.add("top");
// ocattr.add("person");
// ocattr.add("inetOrgPerson");
// ocattr.add("organizationalPerson");
// attrs.put(ocattr);
// attrs.put("cn", user.getFullName());
// attrs.put("sn", user.getLastName());
// attrs.put("userPassword", ldapShaPasswordEncoder.encodePassword(user.getPassword(), null));
// return attrs;
// }

public List<String> getAllPersonNames() {
return ldapTemplate.search(
Expand Down Expand Up @@ -106,8 +105,16 @@ public User findUser(User user) {

public void create(User user) throws NameAlreadyBoundException {
Name dn = buildDn(user);

ldapTemplate.bind(dn, null, buildAttributes(user));

// use DirContextAdapter instead of buildAttributes
DirContextAdapter context = new DirContextAdapter(dn);

context.setAttributeValues("objectclass", new String[] {"organizationalPerson", "inetOrgPerson", "top", "person"});
context.setAttributeValue("cn", user.getFullName());
context.setAttributeValue("sn", user.getLastName());
context.setAttributeValue("userPassword", ldapShaPasswordEncoder.encodePassword(user.getPassword(), null));

ldapTemplate.bind(context);
}

public void delete(User user) throws NameNotFoundException {
Expand Down

0 comments on commit 3ac2dcd

Please sign in to comment.