BAEL-705: spring-ldap code (#1299)

* WatchService vs. Apache Commons IO Mnitoring

* Indentation fixed

* Indentation fixed

* JAX-RS API using Jersey [BAEL-558]

* JAX-RS API using Jersey [BAEL-558]

* Modifications made to remove xml

* applicationContext.xml removed

* All try catch moved to ExceptionMapper

* fixes

* review comments incorporated

* module renamed

* JAX-RS client [BAEL-595]

* jersey-core dependency removed

* assert changed to assertEquals

* messagebody readers and writers removed

* pom dependency corrected and other minor changes

* Jersey version changed and toString() changed to valueOf()

* BAEL-705: Spring Ldap code

* BAEL-705: Spring Ldap code tab prob rectified

* BAEL-705: Spring Ldap code, readme fixed

* review comments incorporated
This commit is contained in:
Saptarshi Basu
2017-03-07 02:38:01 +05:30
committed by Zeger Hendrikse
parent 7a92909566
commit 7355266feb
12 changed files with 498 additions and 0 deletions
@@ -0,0 +1,80 @@
package com.baeldung.ldap.client;
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
private Environment env;
@Autowired
private ContextSource contextSource;
@Autowired
private LdapTemplate ldapTemplate;
public void authenticate(final String username, final String password) {
contextSource.getContext("cn=" + username + ",ou=users," + env.getRequiredProperty("ldap.partitionSuffix"), password);
}
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;
}
public void create(final String username, final String password) {
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" });
context.setAttributeValue("cn", username);
context.setAttributeValue("sn", username);
context.setAttributeValue("userPassword", digestSHA(password));
ldapTemplate.bind(context);
}
public void modify(final String username, final String password) {
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" });
context.setAttributeValue("cn", username);
context.setAttributeValue("sn", username);
context.setAttributeValue("userPassword", digestSHA(password));
ldapTemplate.modifyAttributes(context);
}
private String digestSHA(final String password) {
String base64;
try {
MessageDigest digest = MessageDigest.getInstance("SHA");
digest.update(password.getBytes());
base64 = Base64.getEncoder().encodeToString(digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return "{SHA}" + base64;
}
}
@@ -0,0 +1,44 @@
package com.baeldung.ldap.javaconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import com.baeldung.ldap.client.LdapClient;
@Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = { "com.baeldung.ldap.*" })
@Profile("default")
public class AppConfig {
@Autowired
private Environment env;
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setBase(env.getRequiredProperty("ldap.partitionSuffix"));
contextSource.setUserDn(env.getRequiredProperty("ldap.principal"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
@Bean
public LdapClient ldapClient() {
return new LdapClient();
}
}
@@ -0,0 +1,6 @@
ldap.partitionSuffix=dc=example,dc=com
ldap.partition=example
ldap.principal=uid=admin,ou=system
ldap.password=secret
ldap.port=18889
ldap.url=ldap://localhost:18889
@@ -0,0 +1,15 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} -
%message%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>