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:
parent
7a92909566
commit
7355266feb
1
pom.xml
1
pom.xml
|
@ -137,6 +137,7 @@
|
|||
<module>spring-jooq</module>
|
||||
<module>spring-jpa</module>
|
||||
<module>spring-katharsis</module>
|
||||
<module>spring-ldap</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-mvc-email</module>
|
||||
<module>spring-mvc-forms</module>
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -0,0 +1,5 @@
|
|||
=========
|
||||
|
||||
## Spring LDAP Example Project
|
||||
- (http://www.baeldung.com/spring-ldap-overview/)
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-ldap</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<junit.version>4.12</junit.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
|
||||
<spring-ldap.version>2.3.1.RELEASE</spring-ldap.version>
|
||||
<jcl.slf4j.version>1.7.22</jcl.slf4j.version>
|
||||
<logback.version>1.1.8</logback.version>
|
||||
<spring-context.version>4.3.6.RELEASE</spring-context.version>
|
||||
<apacheds.version>1.5.5</apacheds.version>
|
||||
<shared-ldap.version>0.9.15</shared-ldap.version>
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>spring-ldap</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.ldap</groupId>
|
||||
<artifactId>spring-ldap-core</artifactId>
|
||||
<version>${spring-ldap.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${jcl.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-context.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- junit -->
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- spring ldap test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.ldap</groupId>
|
||||
<artifactId>spring-ldap-test</artifactId>
|
||||
<version>${spring-ldap.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- apacheds for test -->
|
||||
<dependency>
|
||||
<groupId>org.apache.directory.server</groupId>
|
||||
<artifactId>apacheds-core</artifactId>
|
||||
<version>${apacheds.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.directory.server</groupId>
|
||||
<artifactId>apacheds-core-entry</artifactId>
|
||||
<version>${apacheds.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.directory.server</groupId>
|
||||
<artifactId>apacheds-protocol-shared</artifactId>
|
||||
<version>${apacheds.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.directory.server</groupId>
|
||||
<artifactId>apacheds-protocol-ldap</artifactId>
|
||||
<version>${apacheds.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.directory.server</groupId>
|
||||
<artifactId>apacheds-server-jndi</artifactId>
|
||||
<version>${apacheds.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.directory.shared</groupId>
|
||||
<artifactId>shared-ldap</artifactId>
|
||||
<version>${shared-ldap.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>live</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*LiveTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
|
@ -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>
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.ldap.client;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.AuthenticationException;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.ldap.javaconfig.TestConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("testlive")
|
||||
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class LdapClientLiveTest {
|
||||
|
||||
private static final String USER2 = "TEST02";
|
||||
private static final String USER3 = "TEST03";
|
||||
private static final String USER4 = "TEST04";
|
||||
|
||||
private static final String USER2_PWD = "TEST02";
|
||||
private static final String USER3_PWD = "TEST03";
|
||||
private static final String USER4_PWD = "TEST04";
|
||||
|
||||
private static final String SEARCH_STRING = "TEST*";
|
||||
|
||||
@Autowired
|
||||
private LdapClient ldapClient;
|
||||
|
||||
@Test
|
||||
public void givenLdapClient_whenCorrectCredentials_thenSuccessfulLogin() {
|
||||
ldapClient.authenticate(USER3, USER3_PWD);
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationException.class)
|
||||
public void givenLdapClient_whenIncorrectCredentials_thenFailedLogin() {
|
||||
ldapClient.authenticate(USER3, USER2_PWD);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLdapClient_whenCorrectSearchFilter_thenEntriesReturned() {
|
||||
List<String> users = ldapClient.search(SEARCH_STRING);
|
||||
Assert.assertThat(users, Matchers.containsInAnyOrder(USER2, USER3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLdapClientNotExists_whenDataProvided_thenNewUserCreated() {
|
||||
ldapClient.create(USER4, USER4_PWD);
|
||||
ldapClient.authenticate(USER4, USER4_PWD);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLdapClientExists_whenDataProvided_thenExistingUserModified() {
|
||||
ldapClient.modify(USER2, USER3_PWD);
|
||||
ldapClient.authenticate(USER2, USER3_PWD);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
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.core.io.ResourceLoader;
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
import org.springframework.ldap.core.support.LdapContextSource;
|
||||
import org.springframework.ldap.test.TestContextSourceFactoryBean;
|
||||
|
||||
import com.baeldung.ldap.client.LdapClient;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:test_application.properties")
|
||||
@ComponentScan(basePackages = { "com.baeldung.ldap.*" })
|
||||
@Profile("testlive")
|
||||
public class TestConfig {
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Autowired
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
@Bean
|
||||
public TestContextSourceFactoryBean testContextSource() {
|
||||
TestContextSourceFactoryBean contextSource = new TestContextSourceFactoryBean();
|
||||
contextSource.setDefaultPartitionName(env.getRequiredProperty("ldap.partition"));
|
||||
contextSource.setDefaultPartitionSuffix(env.getRequiredProperty("ldap.partitionSuffix"));
|
||||
contextSource.setPrincipal(env.getRequiredProperty("ldap.principal"));
|
||||
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
|
||||
contextSource.setLdifFile(resourceLoader.getResource(env.getRequiredProperty("ldap.ldiffile")));
|
||||
contextSource.setPort(Integer.valueOf(env.getRequiredProperty("ldap.port")));
|
||||
return contextSource;
|
||||
}
|
||||
|
||||
@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,24 @@
|
|||
version: 1
|
||||
dn: ou=users,dc=example,dc=com
|
||||
objectClass: organizationalUnit
|
||||
objectClass: top
|
||||
ou: users
|
||||
|
||||
dn: cn=TEST03,ou=users,dc=example,dc=com
|
||||
objectClass: inetOrgPerson
|
||||
objectClass: organizationalPerson
|
||||
objectClass: person
|
||||
objectClass: top
|
||||
cn: TEST03
|
||||
sn: TEST03
|
||||
userPassword:: e1NIQX1JbktFOFY2enBpWWdMY0RYQTYzdXZVNjRGZXc9
|
||||
|
||||
dn: cn=TEST02,ou=users,dc=example,dc=com
|
||||
objectClass: inetOrgPerson
|
||||
objectClass: organizationalPerson
|
||||
objectClass: person
|
||||
objectClass: top
|
||||
cn: TEST02
|
||||
sn: TEST02
|
||||
userPassword:: e1NIQX1uZERKdWNNYnl5a3hWdEkyQzgyRUFlalN1WTQ9
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
ldap.partitionSuffix=dc=example,dc=com
|
||||
ldap.partition=example
|
||||
ldap.principal=uid=admin,ou=system
|
||||
ldap.password=secret
|
||||
ldap.ldiffile=classpath:/test.ldif
|
||||
ldap.port=18888
|
||||
ldap.url=ldap://localhost:18888
|
Loading…
Reference in New Issue