Skip to content

Commit 0656c49

Browse files
author
Vitalii Cherkashyn
committed
ldap communication
1 parent 2ee4510 commit 0656c49

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

ldap/connect/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.cherkashyn.vitalii.ldap</groupId>
5+
<artifactId>connect</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>connect</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<dependencies>
12+
13+
<dependency>
14+
<groupId>com.beust</groupId>
15+
<artifactId>jcommander</artifactId>
16+
<version>1.78</version>
17+
</dependency>
18+
19+
</dependencies>
20+
21+
<build>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-assembly-plugin</artifactId>
26+
<!-- version>2.5.4</version -->
27+
<configuration>
28+
<descriptorRefs>
29+
<descriptorRef>jar-with-dependencies</descriptorRef>
30+
</descriptorRefs>
31+
<archive>
32+
<!-- manifestFile>${project.basedir}/src/main/resources/META-INF/MANIFEST.MF</manifestFile -->
33+
<manifest>
34+
<mainClass>com.cherkashyn.vitalii.ldap.Connect</mainClass>
35+
</manifest>
36+
</archive>
37+
<!-- Remove the "-jar-with-dependencies" at the end of the file -->
38+
<appendAssemblyId>false</appendAssemblyId>
39+
</configuration>
40+
<executions>
41+
<execution>
42+
<goals>
43+
<goal>attached</goal>
44+
</goals>
45+
<phase>package</phase>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.cherkashyn.vitalii.ldap;
2+
3+
import com.beust.jcommander.DynamicParameter;
4+
import com.beust.jcommander.Parameter;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class Arguments {
10+
11+
@Parameter(names = {"--help"}, description = "list of arguments for ldap authentication", help = true)
12+
boolean isHelp;
13+
14+
@Parameter(names = {"-u", "--url"}, description = "url to ldap", required = true)
15+
String url="ldaps://ubs000001.addp.com:636";
16+
17+
@Parameter(names = {"-p", "--password"}, description = "password of user", required = true)
18+
String password="my_password";
19+
20+
@DynamicParameter(names = {"-d"}, description = "parameters for ldap connection: -dkey=value ", required = true)
21+
Map<String, String> properties=new HashMap<String, String>();
22+
23+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.cherkashyn.vitalii.ldap;
2+
3+
import com.beust.jcommander.JCommander;
4+
import com.sun.jndi.ldap.LdapCtxFactory;
5+
6+
import javax.naming.AuthenticationException;
7+
import javax.naming.AuthenticationNotSupportedException;
8+
import javax.naming.Context;
9+
import javax.naming.NamingException;
10+
import javax.naming.directory.DirContext;
11+
import javax.naming.directory.InitialDirContext;
12+
import java.util.HashMap;
13+
import java.util.Hashtable;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
import static com.cherkashyn.vitalii.ldap.Operations.printResults;
18+
19+
public class Connect {
20+
public static void main(String[] args) {
21+
System.out.println("--- begin ---");
22+
23+
Arguments arguments = new Arguments();
24+
JCommander.newBuilder()
25+
.addObject(arguments)
26+
.build()
27+
.parse(args);
28+
if (arguments.isHelp) {
29+
new JCommander(arguments, args).usage();
30+
System.exit(1);
31+
}
32+
33+
connectToLdap(getContextEnvironment(arguments.url, arguments.password, arguments.properties), new Operations.SearchByName("VCherkashyn"));
34+
35+
System.out.println("--- end ---");
36+
}
37+
38+
private static void connectToLdap(Map<String, String> environment, Operations.Operation ... operations) {
39+
Hashtable<String, String> env = new Hashtable<String, String>();
40+
env.putAll(environment);
41+
DirContext context = null;
42+
try {
43+
context = new InitialDirContext(env);
44+
System.out.println("Connected... "+environment);
45+
if(operations.length>0){
46+
for(Operations.Operation eachOperation : operations){
47+
printResults(eachOperation.execute(context));
48+
}
49+
}
50+
} catch (AuthenticationNotSupportedException ex) {
51+
System.out.println("The authentication is not supported by the server:"+ex.getMessage());
52+
System.exit(2);
53+
} catch (AuthenticationException ex) {
54+
System.out.println("Incorrect password or username:"+ex.getMessage());
55+
System.exit(3);
56+
} catch (NamingException ex) {
57+
System.out.println("Error when trying to create the context:"+ex.getMessage());
58+
System.exit(4);
59+
}finally {
60+
try {
61+
context.close();
62+
} catch (NamingException e) {
63+
} catch (NullPointerException e){
64+
}
65+
}
66+
67+
}
68+
private static Map<String, String> getContextEnvironment(String url, String userPassword, Map<String, String> properties) {
69+
final Map<String, String> env = new HashMap<String, String>();
70+
env.put(Context.INITIAL_CONTEXT_FACTORY, LdapCtxFactory.class.getName());
71+
env.put(Context.PROVIDER_URL, url);
72+
env.put(Context.SECURITY_PROTOCOL, "ssl");
73+
env.put(Context.SECURITY_AUTHENTICATION, "simple");
74+
env.put(Context.SECURITY_CREDENTIALS, userPassword);
75+
// env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
76+
env.putAll(properties);
77+
return env;
78+
}
79+
80+
private static Map<String, String> convertToMap(List<String> elements) {
81+
Map<String, String> returnValue = new HashMap<String, String>();
82+
String key = null;
83+
for (int index = 0; index < elements.size(); index++) {
84+
if (key == null) {
85+
key = elements.get(index);
86+
} else {
87+
returnValue.put(key, elements.get(index));
88+
key = null;
89+
}
90+
}
91+
return returnValue;
92+
}
93+
94+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.cherkashyn.vitalii.ldap;
2+
3+
import javax.naming.NamingEnumeration;
4+
import javax.naming.NamingException;
5+
import javax.naming.directory.*;
6+
7+
public class Operations {
8+
static interface Operation {
9+
NamingEnumeration<SearchResult> execute(DirContext context) throws NamingException;
10+
}
11+
12+
static void printResults(NamingEnumeration<SearchResult> results) throws NamingException {
13+
while(results.hasMoreElements()){
14+
System.out.println("-----");
15+
SearchResult result = results.next();
16+
Attributes attrs = result.getAttributes();
17+
NamingEnumeration<? extends Attribute> attributes = attrs.getAll();
18+
while(attributes.hasMoreElements()){
19+
Attribute attribute = attributes.next();
20+
System.out.println(attribute.toString());
21+
System.out.println(attribute.get());
22+
}
23+
}
24+
}
25+
26+
static class SearchByName implements Operation {
27+
28+
private final String userName;
29+
30+
SearchByName(String userName){
31+
this.userName = userName;
32+
}
33+
34+
public NamingEnumeration<SearchResult> execute(DirContext context) throws NamingException {
35+
// Specify the search filter
36+
String filterExpression = "(&(objectClass=Person) ((sAMAccountName=" + userName + ")))";
37+
38+
// limit returned attributes to those we care about
39+
String[] attrIDs = {"sn", "givenName"};
40+
41+
SearchControls constraints = new SearchControls();
42+
constraints.setReturningAttributes(attrIDs);
43+
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
44+
45+
// Search for objects using filter and controls
46+
String contextName = "DC=org";
47+
return context.search(contextName, filterExpression, constraints);
48+
}
49+
}
50+
}
51+
52+
53+
// -------------------------------------
54+
// LDAP operations
55+
// -------------------------------------
56+
// Specify the search filter
57+
// String FILTER = "(&(objectClass=Person) ((sAMAccountName=" + user.getUsername() + ")))";
58+
//
59+
// // limit returned attributes to those we care about
60+
// String[] attrIDs = { "sn", "givenName" };
61+
//
62+
// SearchControls ctls = new SearchControls();
63+
//ctls.setReturningAttributes(attrIDs);
64+
//ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
65+
//
66+
// // Search for objects using filter and controls
67+
// NamingEnumeration answer = ctx.search(searchBase, FILTER, ctls);
68+
//
69+
//...
70+
//
71+
// SearchResult sr = (SearchResult) answer.next();
72+
// Attributes attrs = sr.getAttributes();
73+
// surName = attrs.get("sn").toString();
74+
// givenName = attrs.get("givenName").toString();
75+
76+
// ------------------------------------

0 commit comments

Comments
 (0)