Skip to content

Commit 7355266

Browse files
saptarshibasuzhendrikse
authored andcommitted
BAEL-705: spring-ldap code (eugenp#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
1 parent 7a92909 commit 7355266

12 files changed

Lines changed: 498 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<module>spring-jooq</module>
138138
<module>spring-jpa</module>
139139
<module>spring-katharsis</module>
140+
<module>spring-ldap</module>
140141
<module>spring-mockito</module>
141142
<module>spring-mvc-email</module>
142143
<module>spring-mvc-forms</module>

‎spring-ldap/.gitignore‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

‎spring-ldap/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=========
2+
3+
## Spring LDAP Example Project
4+
- (http://www.baeldung.com/spring-ldap-overview/)
5+

‎spring-ldap/pom.xml‎

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-ldap</artifactId>
8+
<version>0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<properties>
12+
<junit.version>4.12</junit.version>
13+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
14+
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
15+
<spring-ldap.version>2.3.1.RELEASE</spring-ldap.version>
16+
<jcl.slf4j.version>1.7.22</jcl.slf4j.version>
17+
<logback.version>1.1.8</logback.version>
18+
<spring-context.version>4.3.6.RELEASE</spring-context.version>
19+
<apacheds.version>1.5.5</apacheds.version>
20+
<shared-ldap.version>0.9.15</shared-ldap.version>
21+
<org.hamcrest.version>1.3</org.hamcrest.version>
22+
</properties>
23+
24+
<build>
25+
<finalName>spring-ldap</finalName>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>${maven-compiler-plugin.version}</version>
31+
<configuration>
32+
<source>1.8</source>
33+
<target>1.8</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-surefire-plugin</artifactId>
39+
<version>${maven-surefire-plugin.version}</version>
40+
<configuration>
41+
<excludes>
42+
<exclude>**/*IntegrationTest.java</exclude>
43+
<exclude>**/*LiveTest.java</exclude>
44+
</excludes>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
<dependencies>
51+
52+
<dependency>
53+
<groupId>org.springframework.ldap</groupId>
54+
<artifactId>spring-ldap-core</artifactId>
55+
<version>${spring-ldap.version}</version>
56+
<exclusions>
57+
<exclusion>
58+
<groupId>commons-logging</groupId>
59+
<artifactId>commons-logging</artifactId>
60+
</exclusion>
61+
</exclusions>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.slf4j</groupId>
66+
<artifactId>jcl-over-slf4j</artifactId>
67+
<version>${jcl.slf4j.version}</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>ch.qos.logback</groupId>
72+
<artifactId>logback-classic</artifactId>
73+
<version>${logback.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.springframework</groupId>
77+
<artifactId>spring-context</artifactId>
78+
<version>${spring-context.version}</version>
79+
</dependency>
80+
81+
82+
<!-- junit -->
83+
84+
<dependency>
85+
<groupId>junit</groupId>
86+
<artifactId>junit</artifactId>
87+
<version>${junit.version}</version>
88+
<scope>test</scope>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>org.hamcrest</groupId>
93+
<artifactId>hamcrest-core</artifactId>
94+
<version>${org.hamcrest.version}</version>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.hamcrest</groupId>
99+
<artifactId>hamcrest-library</artifactId>
100+
<version>${org.hamcrest.version}</version>
101+
<scope>test</scope>
102+
</dependency>
103+
104+
<!-- spring ldap test -->
105+
<dependency>
106+
<groupId>org.springframework.ldap</groupId>
107+
<artifactId>spring-ldap-test</artifactId>
108+
<version>${spring-ldap.version}</version>
109+
<scope>test</scope>
110+
</dependency>
111+
112+
<!-- apacheds for test -->
113+
<dependency>
114+
<groupId>org.apache.directory.server</groupId>
115+
<artifactId>apacheds-core</artifactId>
116+
<version>${apacheds.version}</version>
117+
<scope>test</scope>
118+
</dependency>
119+
<dependency>
120+
<groupId>org.apache.directory.server</groupId>
121+
<artifactId>apacheds-core-entry</artifactId>
122+
<version>${apacheds.version}</version>
123+
<scope>test</scope>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.apache.directory.server</groupId>
127+
<artifactId>apacheds-protocol-shared</artifactId>
128+
<version>${apacheds.version}</version>
129+
<scope>test</scope>
130+
</dependency>
131+
<dependency>
132+
<groupId>org.apache.directory.server</groupId>
133+
<artifactId>apacheds-protocol-ldap</artifactId>
134+
<version>${apacheds.version}</version>
135+
<scope>test</scope>
136+
</dependency>
137+
<dependency>
138+
<groupId>org.apache.directory.server</groupId>
139+
<artifactId>apacheds-server-jndi</artifactId>
140+
<version>${apacheds.version}</version>
141+
<scope>test</scope>
142+
</dependency>
143+
<dependency>
144+
<groupId>org.apache.directory.shared</groupId>
145+
<artifactId>shared-ldap</artifactId>
146+
<version>${shared-ldap.version}</version>
147+
<scope>test</scope>
148+
</dependency>
149+
150+
</dependencies>
151+
152+
<profiles>
153+
<profile>
154+
<id>live</id>
155+
<build>
156+
<plugins>
157+
<plugin>
158+
<groupId>org.apache.maven.plugins</groupId>
159+
<artifactId>maven-surefire-plugin</artifactId>
160+
<executions>
161+
<execution>
162+
<phase>integration-test</phase>
163+
<goals>
164+
<goal>test</goal>
165+
</goals>
166+
<configuration>
167+
<excludes>
168+
<exclude>**/*IntegrationTest.java</exclude>
169+
</excludes>
170+
<includes>
171+
<include>**/*LiveTest.java</include>
172+
</includes>
173+
</configuration>
174+
</execution>
175+
</executions>
176+
</plugin>
177+
</plugins>
178+
</build>
179+
</profile>
180+
</profiles>
181+
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.ldap.client;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.util.Base64;
6+
import java.util.List;
7+
8+
import javax.naming.Name;
9+
import javax.naming.NamingException;
10+
import javax.naming.directory.Attributes;
11+
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.core.env.Environment;
14+
import org.springframework.ldap.core.AttributesMapper;
15+
import org.springframework.ldap.core.ContextSource;
16+
import org.springframework.ldap.core.DirContextAdapter;
17+
import org.springframework.ldap.core.DirContextOperations;
18+
import org.springframework.ldap.core.LdapTemplate;
19+
import org.springframework.ldap.support.LdapNameBuilder;
20+
21+
public class LdapClient {
22+
23+
@Autowired
24+
private Environment env;
25+
26+
@Autowired
27+
private ContextSource contextSource;
28+
29+
@Autowired
30+
private LdapTemplate ldapTemplate;
31+
32+
public void authenticate(final String username, final String password) {
33+
contextSource.getContext("cn=" + username + ",ou=users," + env.getRequiredProperty("ldap.partitionSuffix"), password);
34+
}
35+
36+
public List<String> search(final String username) {
37+
List<String> users = ldapTemplate.search("ou=users", "cn=" + username, new AttributesMapper<String>() {
38+
public String mapFromAttributes(Attributes attrs) throws NamingException {
39+
return (String) attrs.get("cn").get();
40+
}
41+
});
42+
return users;
43+
}
44+
45+
public void create(final String username, final String password) {
46+
Name dn = LdapNameBuilder.newInstance().add("ou", "users").add("cn", username).build();
47+
DirContextAdapter context = new DirContextAdapter(dn);
48+
49+
context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
50+
context.setAttributeValue("cn", username);
51+
context.setAttributeValue("sn", username);
52+
context.setAttributeValue("userPassword", digestSHA(password));
53+
54+
ldapTemplate.bind(context);
55+
}
56+
57+
public void modify(final String username, final String password) {
58+
Name dn = LdapNameBuilder.newInstance().add("ou", "users").add("cn", username).build();
59+
DirContextOperations context = ldapTemplate.lookupContext(dn);
60+
61+
context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
62+
context.setAttributeValue("cn", username);
63+
context.setAttributeValue("sn", username);
64+
context.setAttributeValue("userPassword", digestSHA(password));
65+
66+
ldapTemplate.modifyAttributes(context);
67+
}
68+
69+
private String digestSHA(final String password) {
70+
String base64;
71+
try {
72+
MessageDigest digest = MessageDigest.getInstance("SHA");
73+
digest.update(password.getBytes());
74+
base64 = Base64.getEncoder().encodeToString(digest.digest());
75+
} catch (NoSuchAlgorithmException e) {
76+
throw new RuntimeException(e);
77+
}
78+
return "{SHA}" + base64;
79+
}
80+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.ldap.javaconfig;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Profile;
8+
import org.springframework.context.annotation.PropertySource;
9+
import org.springframework.core.env.Environment;
10+
import org.springframework.ldap.core.LdapTemplate;
11+
import org.springframework.ldap.core.support.LdapContextSource;
12+
13+
import com.baeldung.ldap.client.LdapClient;
14+
15+
@Configuration
16+
@PropertySource("classpath:application.properties")
17+
@ComponentScan(basePackages = { "com.baeldung.ldap.*" })
18+
@Profile("default")
19+
public class AppConfig {
20+
21+
@Autowired
22+
private Environment env;
23+
24+
@Bean
25+
public LdapContextSource contextSource() {
26+
LdapContextSource contextSource = new LdapContextSource();
27+
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
28+
contextSource.setBase(env.getRequiredProperty("ldap.partitionSuffix"));
29+
contextSource.setUserDn(env.getRequiredProperty("ldap.principal"));
30+
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
31+
return contextSource;
32+
}
33+
34+
@Bean
35+
public LdapTemplate ldapTemplate() {
36+
return new LdapTemplate(contextSource());
37+
}
38+
39+
@Bean
40+
public LdapClient ldapClient() {
41+
return new LdapClient();
42+
}
43+
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ldap.partitionSuffix=dc=example,dc=com
2+
ldap.partition=example
3+
ldap.principal=uid=admin,ou=system
4+
ldap.password=secret
5+
ldap.port=18889
6+
ldap.url=ldap://localhost:18889
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} -
6+
%message%n
7+
</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="INFO">
12+
<appender-ref ref="STDOUT" />
13+
</root>
14+
15+
</configuration>

0 commit comments

Comments
 (0)