Refactor LDAPClient
package com.baeldung.ldap.client;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
+import org.springframework.ldap.core.*;
+import org.springframework.ldap.support.LdapNameBuilder;
+
+import javax.naming.Name;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
-import javax.naming.Name;
-import javax.naming.NamingException;
-import javax.naming.directory.Attributes;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.env.Environment;
-import org.springframework.ldap.core.AttributesMapper;
-import org.springframework.ldap.core.ContextSource;
-import org.springframework.ldap.core.DirContextAdapter;
-import org.springframework.ldap.core.DirContextOperations;
-import org.springframework.ldap.core.LdapTemplate;
-import org.springframework.ldap.support.LdapNameBuilder;
-
public class LdapClient {
@Autowired
@@ -34,16 +27,20 @@ public class LdapClient {
}
public List<String> search(final String username) {
- List<String> users = ldapTemplate.search("ou=users", "cn=" + username, new AttributesMapper<String>() {
- public String mapFromAttributes(Attributes attrs) throws NamingException {
- return (String) attrs.get("cn").get();
- }
- });
- return users;
+ return ldapTemplate.search(
+ "ou=users",
+ "cn=" + username,
+ (AttributesMapper<String>) attrs -> (String) attrs
+ .get("cn")
+ .get());
}
public void create(final String username, final String password) {
- Name dn = LdapNameBuilder.newInstance().add("ou", "users").add("cn", username).build();
+ Name dn = LdapNameBuilder
+ .newInstance()
+ .add("ou", "users")
+ .add("cn", username)
+ .build();
DirContextAdapter context = new DirContextAdapter(dn);
context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
@@ -55,7 +52,11 @@ public class LdapClient {
}
public void modify(final String username, final String password) {
- Name dn = LdapNameBuilder.newInstance().add("ou", "users").add("cn", username).build();
+ Name dn = LdapNameBuilder
+ .newInstance()
+ .add("ou", "users")
+ .add("cn", username)
+ .build();
DirContextOperations context = ldapTemplate.lookupContext(dn);
context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
@@ -71,7 +72,9 @@ public class LdapClient {
try {
MessageDigest digest = MessageDigest.getInstance("SHA");
digest.update(password.getBytes());
- base64 = Base64.getEncoder().encodeToString(digest.digest());
+ base64 = Base64
+ .getEncoder()
+ .encodeToString(digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
